[英]JFrame without frame border, maximum button, minimum button and frame icon
我想創建一個無框架邊框,最大按鈕,最小按鈕和框架圖標。
在JFrame
上調用setUndecorated(true)
。
只有在幀不可顯示時才能調用此方法(請參閱JavaDoc )。
此代碼說明了如何實現它。
注意: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);
}
}
在構造函數內部你可以把代碼setUndecorated(true)它將消失Frame。
例如://這是構造函數
public freak() {
super("Images");
panel = new JPanel();
ImageIcon image = new ImageIcon("C:\\Users\\shilu.shilu-PC\\Desktop\\2.jpg");
label = new JLabel(image);
add(panel);
add(label);
//Main thing is this one
setUndecorated(true);
//Just add this code in your constructor
}
你可以使用java.awt.Window
類。 Window
就像一個JFrame
,但沒有邊框。
請注意, Window
類構造函數需要Frame
( java.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
的實例MyWindow
不是Window
。
public static void main (String[] args) {
Window window = new MyWindow();
// Other stuff in main
}
我希望這有幫助!
使用方法frame.getContentPane(); 此方法返回任何幀的內容。 但是你需要把它投射到JPanel中。 PrintUI使用JPanel而不是JFrame ....
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.