返回本章節
返回作業目錄
需求說明:
使用JFrame創建一個窗體
實現思路:
使用JFrame創建窗體的思路
定義一個窗體對象f,窗體名稱爲“一個簡單窗口”
設置窗體左上角與顯示屏左上角的座標
//離顯示屏上邊緣300像素,離顯示屏左邊緣300像素
f.setLocation(300,300);
設置窗體的大小
設置窗體可見
用戶單擊窗口的關閉按鈕時程序執行的操作
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
實現代碼:
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("一個簡單窗口");
f.setLocation(300,300);
f.setSize(600, 400);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
}