1

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);
        }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • 1
    1) Why would you want to open multiple `JFrame`s? They create a horrible UX, see [The use of multiple JFrames, Good / Bad practice?](https://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) (Bad). 2) Follow [Java naming conventions](https://www.oracle.com/technetwork/java/codeconventions-135099.html) i.e. `firstWordLowerCaseVariable`, `firstWordLowerCaseMethod()`, `FirstWordUpperCaseClass` and `ALL_WORDS_UPPER_CASE_CONSTANT` and they should have meaningful names (`AL` means nothing to me in a longer program). – Frakcool Nov 08 '19 at 16:48
  • 1
    3) Why are you using a static variable for your `JFrame`? 4) Don't extend `JFrame` directly. 5) Why you leave so much space between your method declaration and your method body and same at the end, no need for extra space after or before curly braces `{}`. 6) Your issue is related to point #3, about you using a `static` variable to keep track of the number of windows you've opened. You should have a shared model instead to track that – Frakcool Nov 08 '19 at 16:50

1 Answers1

1

The issue here is that we are using a static modifier for bt1. With the static keyword, we create a new button the first time the frame constructor is called. On each subsqeuent call to the frame constructor, a new button is not created. Rather the static keyword causes the button to only be created once, on the first invocation.

Every time you create a new JFrame, it is actually using the same button for each of the JFrames. This would in turn cause the behavior you are seeing.

When you create your first frame, it has the bt1 attached to it. When you click next, it will create a second frame with the same bt1 attached it. More importantly, when you called this constructor, you created a new ActionListener and attached it to the same button. So every time you call this constructor, you will be adding another listener to the same button object that is being used across all frames.

The root of the issue is that, with every constuctor, we are adding a new ActionListener to the same button.

I believe you could solve this issue in one of two ways.

  1. Make bt1 non-static such that each frame object has its own unique button
  2. Make your actionListener in the frame constructor static. In this case all frames will be using the same button. And all buttons will be using the same (and singular) actionListener.
Neil Ruggiero
  • 330
  • 3
  • 4
  • 11
  • @ Frakcool - I would agree with that statement. In general this is bad practice, but I don't know the entire context of the users scenario. So I can't say for sure whether thats a good or bad option. @Elias Be aware that using the static keyword can be dangerous and should be used with caution. – Neil Ruggiero Nov 08 '19 at 18:27