2010-05-06 18 views
9

我想定製我的標題欄,最小化,最大化和關閉按鈕。所以我在我的JFrame上使用setUndecorated(true);,但我仍然希望能夠調整窗口大小。什麼是最好的實現方式?如何在使用未裝飾的JFrame時添加對調整大小的支持?

我在RootPane上有一個邊框,我可以在Border或RootPane上使用MouseListeners。任何建議?

import java.awt.Color; 

import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.border.LineBorder; 

public class UndecoratedFrame extends JFrame { 

    private LineBorder border = new LineBorder(Color.BLUE,2); 
    private JMenuBar menuBar = new JMenuBar(); 
    private JMenu menu = new JMenu("File"); 
    private JMenuItem item = new JMenuItem("Nothing"); 

    public UndecoratedFrame() { 
     menu.add(item); 
     menuBar.add(menu); 
     this.setJMenuBar(menuBar); 
     this.setUndecorated(true); 
     this.getRootPane().setBorder(border); 
     this.setSize(400,340); 
     this.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new UndecoratedFrame(); 
    } 
} 
+0

**相關問題:** http://stackoverflow.com/questions/2781987/how-can-i-customize-the-title-bar-on-jframe – Jonas 2010-05-06 21:26:09

+0

**這裏還有一個[示例]( http://stackoverflow.com/a/24476755/2587435)** – 2015-01-08 05:29:23

回答

6

正如您所說,您的根窗格上有一個邊框。因此,至少有一個位置(在繪製邊框的位置的下面),其中根窗格是最上面的組件。因此,您可以添加鼠標偵聽器和鼠標移動偵聽器。

當您單擊根窗格(並按下鼠標按鈕)時,您的鼠標和動作偵聽器會通知您初始和實際的鼠標位置。因此,您可以更新兩個值之間的偏移幀大小,使您的幀可調整大小。

+0

這是一個非常微妙的答案。 – smwikipedia 2015-04-01 09:00:04

5

我在RootPane中發現了一個很好的方法,它給了我這個功能,所以現在我只需要找出如何自定義標題欄和按鈕就可以了。

我在我的構造函數中爲UndecoratedFrame添加了this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);

有關於這對Sexy Swing App – The Unified ToolbarHow to control window decorations

+0

**更多閱讀:** http://javabyexample.wisdomplug.com/java-concepts/34-core-java/57-spice-up-your-swing-ui-with-substance.html – Jonas 2010-05-06 13:12:21

相關問題