开发者

Java - Only dispose JFrame on close?

开发者 https://www.devze.com 2023-02-04 15:59 出处:网络
In my Java program I would like that, regardless of any other windows being open, whenever the user clicks the red X in the corner, just that swing frame closes. I experimented with JFrame.DISPOSE_ON_

In my Java program I would like that, regardless of any other windows being open, whenever the user clicks the red X in the corner, just that swing frame closes. I experimented with JFrame.DISPOSE_ON_CLOSE and other window listeners, but when I exit one JFrame the system still exited.

To clarify, suppose I have two JFrames visible, exiting one automatically exits the other开发者_StackOverflow中文版, and the system exits. Any ideas as to how to only close one JFrame? Thanks.


This works for me:

public class Test {

    public static class TestFrame implements Runnable{
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(200, 300);
            frame.setVisible(true);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new TestFrame());
        EventQueue.invokeLater(new TestFrame());
    }
}


Finnw was correct - I set a new WindowListener (although it never called System.exit() unless it does by default) but I can handle it moving forward now. Thank you all

0

精彩评论

暂无评论...
验证码 换一张
取 消