5

My JFrame closes when I close another frame

 3 years ago
source link: https://www.codesd.com/item/my-jframe-closes-when-i-close-another-frame.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

My JFrame closes when I close another frame

advertisements

I have a project that I'm working on a project that requires 2 JFrames in a single program. The problem is that when I close one the other will also close so I made a test class to see what the issue was and I still couldn't figure it out so here is the test case that I have:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class frameTest {

    public static void main(String[] args) {

        JFrame f1 = new JFrame();
        JButton open = new JButton("open");
        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                JFrame f2 = new JFrame();
                f2.setVisible(true);
                f2.setDefaultCloseOperation(f2.EXIT_ON_CLOSE);
                f2.setSize(200, 200);
            }
        });

        f1.setDefaultCloseOperation(f1.EXIT_ON_CLOSE);
        f1.setVisible(true);
        f1.setSize(500, 500);
        f1.add(open);
    }
}

When I click the open button the popup (f2) will appear but when I close it the other window will also close, why does this happen?


f2.setDefaultCloseOperation(f2.EXIT_ON_CLOSE);

EXIT_ON_CLOSE means close the Java VM.

If you just want to close the current frame then use:

f2.setDefaultCloseOperation(f2.DISPOSE_ON_CLOSE);


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK