1.Swing 是基于 AWT 的
是以在 Swing 項目中,會引入 java.awt.*
2.控件
awt: Label Button
Swing: JLabel JButton (輕量化)
注意:在程式中使用Label\Button和使用JLabel\JButton都不會報錯,但是,使用Label\Button,中文會産生亂碼。
package com.java.gui02;
import javax.swing.*;
import java.awt.*;
/**
* @author zyx
* @create 2021-05-27 14:27
*
* 1.Swing 是基于 AWT 的
* 是以在 Swing 項目中,會引入 java.awt.*
*
* 2.控件
* awt: Label Button
* Swing: JLabel JButton (輕量化)
*/
public class MyJFrame extends JFrame{
public MyJFrame(String title){
super(title);
Container contentPane = this.getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(new JButton("按鈕"));
contentPane.add(new JLabel("文本"));
}
}
package com.java.gui02;
import javax.swing.*;
/**
* @author zyx
* @create 2021-05-27 14:29
*/
public class MyJFrameTest {
private static void createGUI(){
MyJFrame myJFrame = new MyJFrame("MyJFrame");
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myJFrame.setSize(400,300);
myJFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createGUI();
}
});
}
}