參考狂神b站視頻,希望大家多多關注狂神呀
public class JFrameDemo {
//init() 初始化
public void init() {
//JFrame是頂級窗口
JFrame frame = new JFrame("這是一個JFrame窗口");
frame.setVisible(true);
frame.setBounds(100, 100, 300, 300);
frame.setBackground(Color.CYAN);
JLabel label = new JLabel("JLabel");
frame.add(label);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一個窗口
new JFrameDemo().init();
}
}
public class JFrameDemo02 {
public static void main(String[] args) {
new MyJFrame2().init();
}
}
class MyJFrame2 extends JFrame {
public void init() {
this.setBounds(100, 100, 300, 300);
this.setVisible(true);
JLabel label = new JLabel("JLabel");
this.add(label);
label.setHorizontalAlignment(SwingConstants.CENTER);
Container container = this.getContentPane();
container.setBackground(Color.YELLOW);
}
}