如何禁用对话框中的 X?

发布于 2024-10-21 23:50:21 字数 675 浏览 3 评论 0原文

有没有办法禁用对话框上的所有 X?

我可以通过以下方式完成这一任务:

JOptionPane pane = new JOptionPane("Are you hungry?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);

JDialog dialog = pane.createDialog("Title"); 
dialog.addWindowListener(new WindowAdapter() {    
public void windowClosing(WindowEvent evt) {
    } }); 
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 
dialog.pack();

但问题是我必须将其应用到其他 20-40 个对话框,这将非常耗时。

所以我想知道如何用一行代码来制作字体所有对话框上都更大(如下所示),有没有办法禁用 X 功能。

UIManager.put("OptionPane.messageFont", 新的 FontUIResource(新 字体(“ARIAL”,Font.BOLD,30)));

Is there a way to disable all X on dialog boxes?

I can accomplish this on one by doing:

JOptionPane pane = new JOptionPane("Are you hungry?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);

JDialog dialog = pane.createDialog("Title"); 
dialog.addWindowListener(new WindowAdapter() {    
public void windowClosing(WindowEvent evt) {
    } }); 
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 
dialog.pack();

But the problem is thst I have to apply this to other 20-40 dialog boxes which will be time consuming..

So I was wondering just how you can do one line of code to make the fonts bigger on all the Dialog boxes (shown below), is there a way to for the X disable feature.

UIManager.put("OptionPane.messageFont",
new FontUIResource(new
Font("ARIAL",Font.BOLD,30)));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗼ふ静 2024-10-28 23:50:22

为什么不创建一个扩展 JDialog 的自定义类,在构造函数中设置您想要的关闭行为?

class NoCLoseDialog extends JDialog {
  NoCloseDialog(){
    addWindowListener(new WindowAdaptor(){
      // ... Close behavior ...
    });
  }
}

Crtl+F 将 JDialog 替换为 NoCloseDialog ...

Why not just create a custom class that extends the JDialog setting your desired close behavior in the constructor?

class NoCLoseDialog extends JDialog {
  NoCloseDialog(){
    addWindowListener(new WindowAdaptor(){
      // ... Close behavior ...
    });
  }
}

Crtl+F Replace JDialog with NoCloseDialog ...

紫南 2024-10-28 23:50:21

JDialog 方法 setDefaultCloseOperation 的工作方式与 JFrame 的类似:

http://download.oracle.com/javase/6/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)

两者都不会对于所有框架,它只会对它所设置的实例执行此操作。如果您想应用于所有对话框,您需要执行以下操作之一:

  • 每次构造对话框时调用此方法
  • 并拥有它 。为您设置默认值,然后使用该新类
  • 创建一个使用此默认集构造对话框的工厂方法,然后在需要新对话框时调用此方法。

JDialog method setDefaultCloseOperation works just like JFrame's:

http://download.oracle.com/javase/6/docs/api/javax/swing/JDialog.html#setDefaultCloseOperation(int)

Neither will do this for all frames, it will just do it for the instance it is set on. If you want to apply to all your dialogs you need to do one of the following:

  • Call this every time you construct a dialog.
  • Extend JDialog and have it set the default for you, then use that new class.
  • Create a factory method that will construct a dialog with this default set. Then call this method whenever you need a new dialog.
黑色毁心梦 2024-10-28 23:50:21

您需要添加一个 windowListener,还需要设置对话框的默认关闭操作。

    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if (someConditionIsMet) {
                dispose();
            }
        }
    });

You need to add a windowListener but also set the default close operation for the dialog.

    setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if (someConditionIsMet) {
                dispose();
            }
        }
    });
陌上青苔 2024-10-28 23:50:21

您应该将其删除,而不是禁用该控件。用户不应该看到“X”按钮,然后让它表现得不像他们期望的那样。

请参阅:

Rather than disable a control, you should remove it. Users shouldn't see an "X" button and then have it not behave like they expect.

See:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文