2010-01-06 60 views

回答

42

致電setUndecorated(true)在您的JFrame

只能在幀不可顯示的情況下調用此方法(請參閱JavaDoc)。

enter image description here

+0

好吧,我將設置一個答案是一個接受的答案fi解決我的問題。謝謝! – 2010-01-06 09:13:15

+1

@Peter:我怎樣才能讓一個框架不可顯示? 我創建了JFrame並使用Netbeans爲其添加了一些控件。 在主函數I代碼中:MyJframe form = new MyJframe(); form.setUndecorated(true); form.setVisible(true); 但它拋出異常 – 2010-01-06 09:23:01

+0

@Chan - MyJFrame構造函數調用pack()嗎?這將使JFrame可顯示 – willcodejavaforfood 2010-01-06 14:04:36

1

您可以將java.awt.Window類。 A Window就像是JFrame,但沒有邊框。

請注意,Window類構造函數需要Framejava.awt.Frame)作爲參數,但您可以將其設置爲null。您還可以擴展Window類定製這樣的:

public class MyWindow extends Window{ 
    public MyWindow(){ 
     super(null); // creates a window with no Frame as owner 
     setBounds(x, y, width, height); 
     setVisible(true); 
    } 
} 

main,您可以創建MyWindow而不是Window一個實例。

public static void main (String[] args) { 
    Window window = new MyWindow(); 
    // Other stuff in main 
} 

我希望這有助於!

+0

注意2件事:1)這個問題有'swing'標籤應用。 2)Swing提供了一個'JWindow'。如果你刪除或(最好)編輯你的答案,你可能會避免一些倒票。 ;) – 2011-05-22 05:25:39

+0

窗口或Jwindow不支持某些鼠標事件,並且它沒有任務欄控件。 – 2013-05-28 07:29:55

5

此代碼說明如何實現它。

注意:setUndecorated(true);語句在構造函數中。

當幀已被顯示時,您無法將幀解開。

public class MyFrame extends JFrame { 

private JPanel contentPane; 
private JTextField textField; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 

       MyFrame frame = new MyFrame(); 
       frame.setVisible(true); 

} 
/** 
* Create the frame. 
*/ 
public MyFrame() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBackground(Color.ORANGE); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 


    /* important Statement */ 
    setUndecorated(true); 
} 

}

Frame without Border

0

使用方法frame.getContentPane();這個方法返回任何幀內的內容。 但是你需要將它轉換成JPanel。 PrintUI使用JPanel而不是JFrame ....