选项窗格独立于选项停止程序

时间:2018-01-09 13:25:13

标签: java swing joptionpane

所以,我有一个JOptionPane,当我点击JFrame的“red X”时,会弹出确认对话框。问题是,无论我选择什么,它都会停止程序。点击“否”时我怎么也不能停止程序?

此外,如果我打开了两个框架,我如何创建它以仅关闭所选框架?因为它正在关闭两个帧。

if (JOptionPane.showConfirmDialog(null, "Are you sure to close this window?", "Confirm closing",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
    System.exit(0);
}

2 个答案:

答案 0 :(得分:2)

首先你需要设置

frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

然后你需要像这样在你的框架中添加一个WindowListener:

 frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent ev) {
if (JOptionPane.showConfirmDialog(null, "Are you sure to close this window?", "Confirm closing",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
                System.exit(0);
            }

}});

如果您有2个框架,但只需关闭一个框架,则可以使用frame.dispose();代替System.exit(0);

答案 1 :(得分:0)

你应该通过比较相应的枚举来检查showConfirmDialog的结果(返回的值不是布尔值)

:
'postVarSets' => array (
  '_DEFAULT' => array (
    // EXT:news start
    'news' => array(
            array(
                    'GETvar' => 'tx_news_pi1[action]',
                    'noMatch' => 'bypass'
            ),
            array(
                    'GETvar' => 'tx_news_pi1[controller]',
                    'noMatch' => 'bypass'
            ),
            array(
                    'GETvar' => 'tx_news_pi1[news]',
                    'noMatch' => 'bypass',
                    'lookUpTable' => array(
                           :