GUI程式設計
告訴大家怎麼學?
- 這是什麼?
- 他怎麼玩?
- 該如何在平時運用?
元件
- 視窗
- 彈窗
- 面闆
- 文本框
- 清單框
- 按鈕
- 圖檔
- 監聽事件
- 滑鼠
- 鍵盤事件
- 破解工具
1、簡介
Gui的核心技術:Swing AWT
- 因為界面不美觀
- 需要jre環境!
為什麼我們要學習
- 可以寫出自己心中想要的一些小工具
- 工作時候,也可能需要維護到swing界面,機率極小!
- 了解MVC架構,了解監聽!
2、AWT
2.1、Awt介紹
- 包含了很多類和接口! GUI:圖形使用者程式設計 Eclipes:Java
- 元素:視窗,按鈕,文本框
- java.awt
2.2、元件和容器
package com.study.lesson01;
import java.awt.*;
//GUI的第一個界面
public class TestFrame {
public static void main(String[] args) {
//Frame,JDK,看源碼!
Frame frame = new Frame("我的第一個Java圖像界面視窗");
//需要設定可見性
frame.setVisible(true);
//設定視窗大小
frame.setSize(400,400);
//設定背景顔色 Color
frame.setBackground(new Color(85,150,68));
//界面的初始位置
frame.setLocation(200,200);
//設定大小固定
frame.setResizable(false);
}
}
效果圖
問題:發現視窗關閉不掉,停止java程式!
嘗試回顧封裝:
package com.study.lesson01;
import java.awt.*;
public class Testframe2 {
public static void main(String[] args) {
//展示多個視窗 new
MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.red);
MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.pink);
}
}
class MyFrame extends Frame{
static int id = 0;//可能存在多個視窗,我們需要一個計數器
public MyFrame(int x,int y,int w,int h,Color color){
super("Myframe"+(++id));
setBackground(color);
setBounds(x,y,w,h);
setVisible(true);
}
}