In the program I want to open many frames when I press a button. every time I press the button I make an instance of the frame and make it visible with the "setVisible" method, but every time I press the button the frames are generated exponentially.
That is to say, if I have 2 open frames and I press the button, 4 more are opened, and if I press it again they open 8 etc.
Here are my codes
public class ex {
public static void main(String[] args) {
frame fr = new frame ();
fr.setVisible(true);
}
}
public class frame extends JFrame{
static int i=1;
static JButton bt1 = new JButton("Next");
public frame () {
super ("Example"+i);
setSize(600,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(400,200);
setResizable(false);
setLayout(new GridLayout(2,1));
JLabel et1 = new JLabel("frame"+i);
this.add(et1);
this.add(bt1);
AL actionListener = new AL ();
bt1.addActionListener(actionListener);
}
}
import java.awt.event.*;
public class AL implements ActionListener{
public void actionPerformed (ActionEvent e) {
if(e.getSource()==frame.bt1) {
frame.i++;
frame fr = new frame ();
fr.setVisible(true);
}
}
}