2013-03-02 24 views
2

我想知道是否有可能展開JFrame,然後只顯示頂部的關閉按鈕或類似的東西..任何幫助將不勝感激。 細節: 其實我正在做一個項目,這是一個基於GUI的軟件,我已經在其中使用JdesktopPane,並且我使用JInternalFrames,因此在頂部顯示標題欄不僅適合我。有什麼辦法可以解決我的問題嗎?只顯示JFrame上的關閉按鈕undecorated

+1

你應該爲此創建一個自定義按鈕。 – tmwanik 2013-03-02 04:13:55

+0

我可以擁有,但是不會顯示在右上角。它可以在那裏顯示? – nsthethunderbolt 2013-03-02 04:15:39

+1

是的,可能是使用佈局管理器。 – tmwanik 2013-03-02 04:19:08

回答

3

基本上,一旦您刪除了邊框,就會對其功能(包括移動)負責。

本示例基本上使用JPanel作爲「標題」窗格的佔位符,並使用BorderLayout來佈局標題和內容。

實際關閉按鈕是一個簡單的JButton,它使用一些圖像來表示關閉操作。顯然,這隻會看的權利在Windows上,這些按鈕都是正常的由OS本身......而我們沒有訪問該層中產生的......

enter image description hereenter image description here

public class TestUndecoratedFrame { 

    public static void main(String[] args) { 
     new TestUndecoratedFrame(); 
    } 

    public TestUndecoratedFrame() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       TitlePane titlePane = new TitlePane(); 

       JFrame frame = new JFrame("Testing"); 
       frame.setUndecorated(true); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       ((JComponent)frame.getContentPane()).setBorder(new LineBorder(Color.BLACK)); 
       frame.add(titlePane, BorderLayout.NORTH); 
       frame.add(new JLabel("This is your content", JLabel.CENTER)); 
       frame.setSize(200, 200); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TitlePane extends JPanel { 

     public TitlePane() { 
      setLayout(new GridBagLayout()); 
      GridBagConstraints gbc = new GridBagConstraints(); 
      gbc.weightx = 1; 
      gbc.anchor = GridBagConstraints.EAST; 
      add(new CloseControl(), gbc); 
     } 
    } 

    public class CloseControl extends JButton { 

     public CloseControl() { 
      setBorderPainted(false); 
      setContentAreaFilled(false); 
      setFocusPainted(false); 
      setOpaque(false); 
      setBorder(new EmptyBorder(0, 0, 8, 12)); 
      try { 
       setRolloverIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Highlighted.png")))); 
       setRolloverEnabled(true); 
       setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/Normal.png")))); 
      } catch (IOException ex) { 
       Logger.getLogger(TestUndecoratedFrame.class.getName()).log(Level.SEVERE, null, ex); 
      } 

      addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        SwingUtilities.getWindowAncestor(CloseControl.this).dispose(); 
       } 
      }); 
     } 
    } 
} 
+0

謝謝..一種新的方式。 – nsthethunderbolt 2013-03-02 05:06:22

+2

@nsthethunderbolt:在這種情況下,您可能可以利用內部框架圖標,見[此處](http://stackoverflow.com/a/10360374/230513)。 – trashgod 2013-03-02 06:40:07

+0

@trashgod +1好主意 – MadProgrammer 2013-03-02 07:29:24