import javax.swing.*;
import java.awt.*;
public class JFrameTest{
public static void main(String[] args) {
new MyJFrame().init();
}
}
class MyJFrame extends JFrame {
public void init() {
this.setVisible(true);
this.setBounds(100, 100, 200, 200);
JLabel jlabel = new JLabel("添加的文字");
this.add(jlabel);
//让文本标签居中,设置水平对齐
jlabel.setHorizontalAlignment(SwingConstants.CENTER);
//获得一个容器
//与Frame不同,JFrame不能直接设置背景颜色,只能通过获得容器再设置这种方式
Container container = this.getContentPane();
container.setBackground(Color.PINK);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
运行结果为:
点击右上角可以关闭窗体