新手學習Java Swing筆記(一)
Swing之基礎使用方法
參考教程:https://blog.csdn.net/xietansheng/article/details/72814531
記下第一天的收穫:
import javax.swing.*;
public class Swing01 {
public static void main(String[] args) {
//創建頂層容器
JFrame frame = new JFrame("This is title!");//創建窗口
frame.setSize(800, 600);//設置大小
frame.setLocationRelativeTo(null);//居中
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//當關閉窗口時結束運行(重點!)
//創建中間容器
JPanel pl = new JPanel(null);//創建面板,並設置爲 絕對佈局
//創建基本組件
JButton button = new JButton("Test");//創建按鈕
button.setSize(200, 150);//設置大小
button.setLocation(100, 450);//設置位置
pl.add(button);//添加按鈕至面板
frame.setContentPane(pl);//將面板設置到窗口
frame.setVisible(true);//顯示窗口
}
}