一、常用窗体
1. JFrame框架窗体
public class TestJFrame {
public void init(){
JFrame jframe = new JFrame("Jframe窗体");
jframe.setBackground(Color.blue);
jframe.setBounds(200,200,500,500);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗体
}
public static void main(String[] args) {
new TestJFrame().init();
}
}
常用的窗体关闭方式有四种:
- “DO_NOTHING_ON_CLOSE” :什么也不做就将窗体关闭;
- “DISPOSE_ON_CLOSE” :任何注册监听程序对象后会自动隐藏并释放窗体;
- “HIDE_ON_CLOSE” : 隐藏窗口的默认窗口关闭;
- “EXIT_ON_CLOSE”:退出应用程序默认窗口关闭。
2、Jlable和Container
public class TestJFrame extends JFrame{
public void init(){
setBackground(Color.blue);
setBounds(200,200,500,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel("这是一个标签");
label.setHorizontalAlignment(SwingConstants.CENTER);//设置标签居中
add(label);
Container contentPane = this.getContentPane();//获得容器
contentPane.add(label);
contentPane.setVisible(true);
contentPane.setBackground(Color.red);
}
public static void main(String[] args) {
new TestJFrame().init();
}
}