java - Is it possible to create new buttons to a frame via action listener? -


This is my code and I would like to know if I can add my grid layout to a button every time I already exist In the example frame of the button.

  Public square board {public static zero main (string [] Args) {Jebton [] Button = New Payton [40]; Int i = 0; Jeffre Frame = New Gefram (); Frame.setLayout (New Grid Layout (20, 20, 15, 15)); While (i & lt; 40) {button [i] = new pocket ("button" + i); Button [i] .addActionListener (new action ()); Frame.add (button [i]); I ++; } Frame.Sets (700, 700); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.setVisible (true); } Static class action implementation ActionListener {@Override Public Zero ActionPerfed (ActionEvent E) {}}} 

The solution is not simple.

What you need to do is think about the problem. First of all, you have a bunch of static references, which are not really necessary and provide you some benefits.

Now, after saying this. To do this you need some way to add the button to verb {/ code>. To do this, verb needs the context of the container that you add to the button Want to ...

  Import java.awt.GridLayout; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing.JButton; Import javax.swing.JFrame; Public square board {public static zero main (string [] args) {new board (;); } Public Board () {Jebton [] Button = New Payton [40]; Int i = 0; Jeffre Frame = New Gefram (); Frame.setLayout (New Grid Layout (20, 20, 15, 15)); Action action = new action (frame); While (i & lt; 40) {button [i] = create button (i); Button [i] .addActionListener (action); Frame.add (button [i]); I ++; } Action.setCount (i); Frame.setSize (700, 700); Frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); Frame.setVisible (true); } Public Button Creation Button (New Index) (New Button ("Button" + Index); } Action of Public Square ActionListener {Private JFrame frame; Private count; Public action (gefrem frame) {this.frame = frame; } Public Zero SetCount (integer number) {this.count = count; } @ Override Public Zero Action Perforated (Action Event E) {Jebton BTN = Seat Button (Count); Btn.addActionListener (this); Frame.add (btn); Frame.revalidate (); Count ++; }}}    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -