JFrame 更加强大 比FrameFrame教程
1. setTitle(“title”); 代替原来的的 setName()
2.拥有内置的退出函数:2个常用的退出.
2.1 正常关闭 EXIT_ON_CLOSE
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
2.2 不能关闭: DO_NOTHING_ON_CLOSE
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
3.JFrame 不能直接改变窗体的颜色,需要线获得一个容器 Container 然后修改 容器的颜色。
package GUI.Swing.JFrame窗体;
import javax.swing.*;
import java.awt.*;
public class 第一个JFrame {
public void init() {
JFrame frame = new JFrame();
//这里使用setTitle 代替 setName()
frame.setTitle("第一个JFrame");
frame.setLocation(100, 100);
frame.setSize(400, 400);
//
Container contentPane = frame.getContentPane();
contentPane.setBackground(new Color(255, 255, 255));
//add JLabel
contentPane.add(new JLabel("JLabelTxt"));
frame.setVisible(true);
//这里有内置的退出方法可以用.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new 第一个JFrame().init();
}
}
设置label的水平居中
//设置水平居中
label.setHorizontalAlignment(SwingConstants.CENTER);