JFrame和Container的使用

1.JFrame(会自动携带关闭监听器):

 1 public static void init() {
 2         JFrame frame = new JFrame("JFrame窗口");
 3         frame.setVisible(true);
 4         frame.setBounds(100,100,200,200);
 5         
 6         //设置文字
 7         JLabel label = new JLabel("JFrame系列");
 8         frame.add(label);
 9         
10         //文本标签居中,设置水平对齐
11         label.setHorizontalAlignment(SwingConstants.CENTER);
12         
13         //关闭事件
14         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
15     }

 

2.Container:

1 class MyJframe2 extends JFrame{
2     public void init() {
3         this.setBounds(10,10,200,300);
4         this.setVisible(true);
5         
6         Container container = this.getContentPane();
7         container.setBackground(Color.blue);
8     }
9 }

 

posted @ 2022-04-09 19:21  jue1e0  阅读(194)  评论(0)    收藏  举报