java 在 JOptionPane 中禁用关闭 x

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1772905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:55:36  来源:igfitidea点击:

Disabling the close x in a JOptionPane

javajoptionpane

提问by Mike2012

I would like to disable the close x in the upper left corner of my JOptionPane how would I do this?

我想禁用 JOptionPane 左上角的 close x 我该怎么做?

采纳答案by Brian Hasden

You could always just show the dialog again when the user tries to close it without selecting an option. There's an example of how to override the default closing behavior at sun.com. Look under "Stopping Automatic Dialog Closing" and they have the following code:

当用户尝试关闭对话框而不选择选项时,您总是可以再次显示对话框。在sun.com 上有一个关于如何覆盖默认关闭行为的示例。查看“停止自动关闭对话框”,它们有以下代码:

final JOptionPane optionPane = new JOptionPane(
                "The only way to close this dialog is by\n"
                + "pressing one of the following buttons.\n"
                + "Do you understand?",
                JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_OPTION);

final JDialog dialog = new JDialog(frame, 
                             "Click a button",
                             true);
dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(
    JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
        setLabel("Thwarted user attempt to close window.");
    }
});

optionPane.addPropertyChangeListener(
    new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) {
            String prop = e.getPropertyName();

            if (dialog.isVisible() 
             && (e.getSource() == optionPane)
             && (prop.equals(JOptionPane.VALUE_PROPERTY))) {
                //If you were going to check something
                //before closing the window, you'd do
                //it here.
                dialog.setVisible(false);
            }
        }
    });
dialog.pack();
dialog.setVisible(true);

int value = ((Integer)optionPane.getValue()).intValue();
if (value == JOptionPane.YES_OPTION) {
    setLabel("Good.");
} else if (value == JOptionPane.NO_OPTION) {
    setLabel("Try using the window decorations "
             + "to close the non-auto-closing dialog. "
             + "You can't!");
}

Using that code, you could easily adapt the commented section to only allow the window to be closed when the user has clicked one of the available options and not the close button.

使用该代码,您可以轻松地调整注释部分,仅在用户单击可用选项之一而不是关闭按钮时才允许关闭窗口。

回答by nandokakimoto

Michael,

迈克尔,

I don't know how to disable the Close[x] button. Alternatively, you can do nothing when user clicks on it. Check the code below:

我不知道如何禁用 Close[x] 按钮。或者,当用户点击它时你什么也不做。检查下面的代码:

JOptionPane pane = new JOptionPane("message");
JDialog dialog = pane.createDialog(null, "Title");
dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
dialog.setVisible(true);

Is it reasonable for you?

对你来说合理吗?

回答by Samsky

you can overwrite your exit button by a cancel button declared in JOptionPane and handle your cancellation operation accordingly:

您可以通过在 JOptionPane 中声明的取消按钮覆盖您的退出按钮,并相应地处理您的取消操作:

JOptionPane optionPane= new JOptionPane("message", JOptionPane.OK_CANCEL_OPTION);

final JDialog dialog = optionPane.createDialog(null, "Input");
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() { 
@Override public void windowClosing(WindowEvent e) { 
    optionPane.setValue(JOptionPane.CANCEL_OPTION);
}
});

if (JOptionPane.CANCEL_OPTION!= ((Integer) optionPane.getValue()).intValue())
                    throw new myCancellationException();

回答by Carl Smotricz

I'm not sure if there is a way to do this in JOptionPane.

我不确定是否有办法在JOptionPane.

Usually when people want more flexibility than JOptionPane offers (it's basically a bunch of static factories for a few dialogs), they write their own dialogs using JDialog.

通常,当人们想要比 JOptionPane 提供的更多灵活性(它基本上是一些对话框的一堆静态工厂)时,他们使用JDialog.

JDialog offers the inherited method setUndecorated, which eliminates the Xaltogether. It's more work but you can make your dialog look however you want.

JDialog 提供了继承的方法setUndecorated,它X完全消除了。这是更多的工作,但你可以让你的对话看起来像你想要的。