Java 删除 Swing JDialog 中的“X”按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/942056/
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
Remove "X" button in Swing JDialog
提问by richs
Is there a way to remove the close button ("X") from the JDialog
title bar?
有没有办法从JDialog
标题栏中删除关闭按钮(“X”)?
回答by Tom Hawtin - tackline
At a guess, set it to be PL&F decorated and remove the component by name.
猜测一下,将其设置为 PL&F 装饰并按名称删除组件。
回答by Huxi
You can remove the whole dialog title by calling dialog.setUndecorated(true)
but this means that the dialog can't be moved anymore.
您可以通过调用删除整个对话框标题,dialog.setUndecorated(true)
但这意味着不能再移动对话框。
You can also execute dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
to prevent that the button does anything.
您还可以执行dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
以防止按钮执行任何操作。
Besides that, I don't think that there's a way to remove the X
completely.
除此之外,我认为没有办法X
完全删除它。
回答by thedude19
I believe that you can call dialog.setUndecorated(true)
to remove the title bar. Not sure about just the 'X' though.
我相信你可以调用dialog.setUndecorated(true)
删除标题栏。虽然不确定只是“X”。
Removing the 'X' may not be a great idea, though, as you want your users to be able to close the dialog easily.
但是,删除“X”可能不是一个好主意,因为您希望用户能够轻松关闭对话框。
Best bet is to control what happens when the users clicks the 'X' by using dialog.setDefaultCloseOperation
or a WindowListener
.
最好的办法是通过使用dialog.setDefaultCloseOperation
或来控制用户单击“X”时发生的情况WindowListener
。
回答by Supuhstar
As of Java 1.7 (AKA Dolphin or Java 7), you can not disable or remove the close button on a Window. You can remove/disable the maximize button with frame.setResizable(false)
and you can remove the minimize and maximize buttons by using a java.awt.Dialog
or a class that extends it, such as javax.swing.JDialog
. You can remove the title bar, borders, and buttons with frame.setUndecorated(true)
, and you can have full control over the visibility of all buttons in the title bar (while losing some cross-platform compatibility and OS integration) with frame.setDefaultLookAndFeelDecorated(true)
(assuming it's a JFrame or JDialog). This is all the control I see possible with the current JDK.
从 Java 1.7(AKA Dolphin 或 Java 7)开始,您无法禁用或删除窗口上的关闭按钮。您可以删除/禁用最大化按钮,frame.setResizable(false)
也可以使用java.awt.Dialog
或 扩展它的类删除最小化和最大化按钮,例如javax.swing.JDialog
. 您可以使用 删除标题栏、边框和按钮frame.setUndecorated(true)
,并且您可以完全控制标题栏中所有按钮的可见性(同时失去一些跨平台兼容性和操作系统集成)frame.setDefaultLookAndFeelDecorated(true)
(假设它是 JFrame 或 JDialog) . 这是我在当前 JDK 中看到的所有可能的控制。
回答by krzydyn
static public void removeButtons(Component c){
if (c instanceof AbstractButton){
String accn = c.getAccessibleContext().getAccessibleName();
Container p=c.getParent();
//log.debug("remove button %s from %s",accn,p.getClass().getName());
c.getParent().remove(c);
}
else if (c instanceof Container){
//log.debug("processing components of %s",c.getClass().getName());
Component[] comps = ((Container)c).getComponents();
for(int i = 0; i<comps.length; ++i)
removeButtons(comps[i]);
}
}
回答by Ryuu
Here is my experience:
这是我的经验:
- Tried using
setUndecorated(true)
: Made the wholeDialog
invisible. - Tried
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
: This didn't change the behavior at all. My dialog box still closed. Setting the default close operation toDO_NOTHING_ON_CLOSE
delegates the close operation to thewindowClosing()
method of a registeredWindowListener
.
- 尝试使用
setUndecorated(true)
:使整个Dialog
不可见。 - 尝试过
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)
:这根本没有改变行为。我的对话框仍然关闭。将默认关闭操作设置为将关闭操作DO_NOTHING_ON_CLOSE
委托windowClosing()
给已注册的WindowListener
.
What worked for me was:
对我有用的是:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
//Remove any existing WindowListeners
for ( WindowListener wl : this.getWindowListeners())
this.removeWindowListener(wl);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if ("Optional condition") {
JOptionPane.showMessageDialog(null, "You cannot close this window");
}
}
});