窗口面板
package com.sean.jframe;
import javax.swing.*;
import java.awt.*;
/**
*
* @create 2021-03-03 10:05
*/
public class Demo {
//init() 初始化
public void init(){
//顶级窗口
JFrame jframe = new JFrame("这是一个jframe窗口");
jframe.setVisible(true);
jframe.setBounds(100,100,200,200);
jframe.setBackground(Color.cyan);
//设置文字 jlabel
JLabel jLabel = new JLabel("欢迎来到我的小窝");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new Demo().init();
}
}
标签居中
package com.sean.jframe;
import javax.swing.*;
import java.awt.*;
/**
*
* @create 2021-03-03 10:14
*/
public class Demo02 {
public static void main(String[] args) {
new MyJframe2().init();
}
}
class MyJframe2 extends JFrame {
public void init(){
this.setBounds(10,10,200,200);
this.setVisible(true);
//设置文字 jlabel
JLabel jLabel = new JLabel("欢迎来到我的小窝");
this.add(jLabel);
//让文本标签居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
}
}