窗口Jframe
初始化窗口 inti();
方法1:(优)
1 package com.luckylu.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 public class JFrameDemo {
7 // init(); 初始化
8 public void init(){
9 //顶级窗口
10 JFrame jf = new JFrame("这是一个JFrame窗口");
11 jf.setVisible(true);
12 jf.setBounds(200,200,300,300);
13 //jf.setBackground(Color.yellow); //设置背景无效,方法
14 //设置文字 JLabel
15 JLabel label = new JLabel("朋友,好久不见!");
16 jf.add(label);
17
18 jf.getContentPane().setBackground(Color.orange); //设置背景颜色
19
20 //让文本居中
21 label.setHorizontalAlignment(SwingConstants.CENTER);
22 //关闭事件
23 jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
24
25 }
26
27 public static void main(String[] args) {
28 new JFrameDemo().init();
29 }
30
31 }
结果:

方法2:
1 package com.luckylu.gui;
2
3 import javax.swing.*;
4 import java.awt.*;
5
6 public class JFrameDemo02 {
7
8 static class MyJframe2 extends JFrame {
9 public void init(){
10 this.setBounds(100,100,300,200);
11 this.setVisible(true);
12
13 JLabel label = new JLabel("欢迎回家!"); //文本标签
14 this.add(label);
15 label.setHorizontalAlignment(SwingConstants.CENTER);
16
17 //获得一个容器
18 Container container = this.getContentPane();
19 container.setBackground(Color.green);
20
21 }
22 }
23 public static void main(String[] args) {
24 new MyJframe2().init();
25 }
26
27 }
结果:

原创文章,作者:1402239773,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/245665.html