【问题标题】:Prevent exit application by closing 1 of the 2 frames通过关闭 2 个框架中的 1 个来防止退出应用程序
【发布时间】:2015-07-04 13:34:38
【问题描述】:

我想要一个可以加载其他程序的简单程序。 到目前为止,它工作得很好,除了如果我关闭一帧所有帧都会关闭(退出代码为 0)。我怎样才能防止这种情况发生?

public class AllSketches extends PApplet {

    public static void main(String[] args) {
        PApplet.main("forMyself.AllSketches", args);
    }

    // here all sketches we want
    P5_BackFaceCulling backFaceCulling;

    public void setup() {

    }

    public void draw() {

    }

    public void keyPressed() {
        if (key == '1') {
            // this creates a new window,
            // but when I close it then both windows gets closed!
          backFaceCulling = new P5_BackFaceCulling();
          backFaceCulling.main(new String[]{});

          Frame f = backFaceCulling.getFrame();

          if (f instanceof JFrame) {
              ((JFrame)f).setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           }
        }
    }

}

为了更清楚: 如果我关闭 2 之一,则程序退出。 我想要的是,如果我关闭右侧的框架,它只会关闭右侧的框架。 (如果我关闭左边的那个,那么我不在乎这种行为)。

【问题讨论】:

    标签: java processing


    【解决方案1】:

    这是由于 Jframe 的setDefaultCloseOperation。你有:

    DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
    HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.
    DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.
    EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications. 
    


    您可能正在使用EXIT_ON_CLOSE。请改用DISPOSE_ON_CLOSE

    【讨论】:

    • 我修改了我的问题而没有看到你的问题。还是谢谢。
    【解决方案2】:

    我加载的草图是用 P3D 制作的。

    因此getFrame() 没有返回JFrame,而是返回某种其他类型的框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      相关资源
      最近更新 更多