I have one window of class Jframe
and it contains one button. On Clicking that button the second window of class Jframe
is opened. On Clicking the close or the exit button of second window then the first window is also closed along with second window.
Why is this happening?
below is my code : // this is my first file
class Frame1 extends JFrame
{
Frame1()
{
super("hello this is window 1");
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JButton btn = new JButton("open second window");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("button clicked");
Frame2 obj2 = new Frame2();
}
});
add(btn);
}
public static void main(String args[])
{
Frame1 obj = new Frame1();
}
}
// this is my second file
class Frame2 extends JFrame
{
Frame2()
{
super("hello this is window 2");
setVisible(true);
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
}
}