Java JFrame 无边框、最大按钮、最小按钮和框架图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2011601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JFrame without frame border, maximum button, minimum button and frame icon
提问by Chan Pye
I would like to create a without frame border, maximum button, minimum button and frame icon.
我想创建一个无边框、最大按钮、最小按钮和框架图标。
采纳答案by Peter Lang
回答by Ramdane
You can the java.awt.Window
class. A Window
is like a JFrame
, but with no borders.
你可以java.awt.Window
上课。AWindow
类似于 a JFrame
,但没有边界。
Note that the Window
class constructor needs a Frame
(java.awt.Frame
) as an argument, but you can set it to null
. You can also extend the Window
class to customize it like this:
请注意,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);
}
}
In main
, you can create an instance of MyWindow
instead of Window
.
在 中main
,您可以创建一个MyWindow
代替 的实例Window
。
public static void main (String[] args) {
Window window = new MyWindow();
// Other stuff in main
}
I hope this helps!
我希望这有帮助!
回答by Vishwanath gowda k
This code Explains how you can achieve it.
此代码解释了如何实现它。
Note: setUndecorated(true); statement in the constructor.
注意:setUndecorated(true); 构造函数中的语句。
You cannot undecorate the Frame while it is already displayed.
当框架已经显示时,您不能取消装饰框架。
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);
}
}
}
回答by Harsh Rawat
Use method frame.getContentPane(); this method return inside content of any frame. But you need to cast it into a JPanel. PrintUI using JPanel not JFrame....
使用方法 frame.getContentPane(); 此方法返回任何框架的内部内容。但是您需要将其转换为 JPanel。PrintUI 使用 JPanel 而不是 JFrame ....
回答by sophin
Inside the constructor you can put code setUndecorated(true) it will disappear Frame.
在构造函数中,您可以放置代码 setUndecorated(true) 它将消失 Frame。
For example: //This is constructor
例如://这是构造函数
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
}