JFrame之关闭事件
import javax.swing.*;
import java.awt.*;
public class JFrameDemo {
//init();初始化
public void init(){
JFrame frame = new JFrame("这是一个JFrame窗口");
frame.setVisible(true);
frame.setBounds(100,100,200,200);
frame.setBackground(Color.cyan);
//设置文字 Jlabel
JLabel label=new JLabel("欢迎来到狂神说Java系列节目");
frame.add(label);
//关闭事件
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args){
//建议一个窗口
new JFrameDemo().init();
}
}
JFrame之文本垂直居中
import javax.swing.*;
import java.awt.*;
public class JframeDemo02 {
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 label= new JLabel("欢迎来到狂神说Java系列节目");
this.add(label);
//文本居中
label.setHorizontalAlignment(SwingConstants.CENTER);
//让我们的文本标签居中
//获得一个容器
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}