I have a class like this:
public class A {
public static void main() {
B f1 = new B();
f1.setVisible(true);
}
class B extends JFrame {
public B() {
JButton btn = new JButton("click me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
C f2 = new C();
f2.setVisible(true);
}
});
add(btn);
}
}
class C extends JFrame {
public C() {
//whatever here
}
}
}
When I first run this java code,
a window X contains a button “click me”. After I click it, another new window Y is popped out.
But the problem is that when I close the new window Y, the old window X is closed together with Y automatically. (i.e. they are closed at the same time)
What I wanna do is that after I close Y, X keeps there and not to be closed. How to do it?
You would change the argument of the
setDefaultCloseOperation()method, which defines what happens when the close button is clicked. In this case, you would useDISPOSE_ON_CLOSEas the argument. The reason both frames are being disposed is presumably because you set the argument toEXIT_ON_CLOSE(if you didn’t set this explicitly, then it was done for you; it is the default close behavior for all frames) which terminates the entire process—this includes all open windows and frames. If you need further assistance, please ask. 🙂