2009-06-02 80 views
52

有没有办法从JDialog标题栏中删除关闭按钮(“X”)?删除Swing JDialog中的“X”按钮

+7

我已经看到了一些应用程序,显示另一个对话框,如果按下关闭按钮告诉您按其他按钮中的一个来代替。天才! – 2009-06-02 22:56:31

回答

1

根据猜测,将其设置为PL & F装饰并按名称删除组件。

+1

什么是PL&F?谢谢。 – kenshinji 2015-05-29 03:01:57

+0

PL&F是可插拔的外观和感觉。 Swing可以采用任意外观和感觉的丑陋方式。 – 2015-05-30 21:30:44

+2

为什么丑陋?什么是正确的方法来做到这一点? – 2015-09-05 18:56:40

53

您可以通过调用dialog.setUndecorated(true)来删除整个对话框标题,但这意味着对话框不能再被移动。

您还可以执行dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)来防止该按钮执行任何操作。

除此之外,我认为没有办法完全去除X.

13

我相信你可以拨打dialog.setUndecorated(true)删除标题栏。虽然不确定只是'X'。

虽然删除'X'可能不是一个好主意,因为您希望用户能够轻松关闭对话框。

最好的办法是控制用户使用dialog.setDefaultCloseOperationWindowListener单击“X”时发生的情况。

9

从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所能实现的所有控制。

-3
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]); 
    } 
} 
4

这里是我的经验:

  • 使用setUndecorated(true)尝试:使整个Dialog无形。
  • 尝试setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE):这并没有改变行为。我的对话框仍然关闭。将默认关闭操作设置为DO_NOTHING_ON_CLOSE可代表对WindowListenerwindowClosing()方法的关闭操作。

什么工作对我来说是:

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"); 
       } 
     } 
});