2014-10-02 133 views
2

在研究問題時,似乎大多數人都想做相反的操作(即刪除最小化/關閉按鈕)。 我已經使用reoccurrent setUndecoratedsetDefaultCloseOperationJDialog未顯示最小化/關閉按鈕

這裏沒有成功是我的代碼:

private class TestDialog extends JDialog 
{ 
    public static final String title_ = "Test Dialog"; 

    public TestDialog(JFrame parent) 
    { 
     super(parent,title_,true); 
     setMinimumSize(new Dimension(500,500)); 
     setLocationRelativeTo(null); 
     setUndecorated(false); 
     setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    } 
} 

當我顯示的對話框中,我得到以下幾點:

enter image description here

其他info:
操作系統:Ubuntu
Java版本:1.7.0_55

+0

是否要刪除關閉/最小化按鈕?爲什麼不從JWindow而不是JDialog繼承? – 2014-10-02 13:51:25

+0

我想要關閉/最小化按鈕出現。我認爲JWindow會因爲沒有嵌入標題欄而不合適。 – Harry 2014-10-02 14:03:25

+0

使用JFrame而不是JDialog – Gladiator 2014-10-02 14:51:56

回答

3

不清楚事情可能發生錯誤的地方,但下面的完整示例適用於Ubuntu 12,Java 6;它可以幫助你確定問題。請注意,必須在event dispatch thread上構建全部top-level container

enter image description here

import java.awt.Dimension; 
import java.awt.EventQueue; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.WindowConstants; 


public class TestDialog extends JDialog { 

    public static final String title = "Test Dialog"; 

    public TestDialog(JFrame parent) { 
     super(parent, title, true); 
     add(new JPanel(){ 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(320, 240); 
      } 
     }); 
     pack(); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    } 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TestDialog(null).setVisible(true); 
      } 
     }); 
    } 
} 
+0

感謝您的回覆@trashgod。嘗試過你的確切例子,我仍然沒有得到你似乎得到的關閉按鈕。我安裝了早期版本的jdk(1.6.0),但仍然沒有。我現在唯一能想到的就是Ubuntu 14.04和/或GNOME的一個問題。 – Harry 2014-10-03 16:11:29

+0

@Harry:在使用Java 7的Ubuntu 14上看起來是一樣的。 – trashgod 2014-10-03 21:21:50

+2

好的。這似乎是GNOME shell的一個問題。當我切換到Unity(默認在Ubuntu)時,它可以工作。感謝您的幫助@trashgod – Harry 2014-10-06 09:35:43

1

我已經找到了各種各樣的治標不治本,如果更改下面一行:

super(parent, title, true); 

super(null, title, Dialog.ModalityType.MODELESS); 

則窗口關閉按鈕就會出現當使用GNOME時。我不知道這可能會導致什麼其他問題。

+0

我很驚訝,編譯。由於有多個匹配該簽名的構造函數,因此我得到一個「模糊的構造函數」錯誤。然而,我發現了一種解決方法:'super(null,title,Dialog.ModalityType.MODELESS);' – Harry 2015-09-02 10:10:54

+1

我的不好,我自己實際上使用了Dialog.ModalityType.MODELESS。我複製了原始示例中的代碼,並忘記更改最後一個參數。 – Fooble 2015-09-02 11:11:16

相關問題