天天看點

GUI程式設計核心技術之一(AWT)

什麼是GUI?

在電腦的作業系統中實作圖形化的使用者界面(Graphical User Interface,簡稱GUI)

GUI元件分為兩大類:基本元件和容器。

注意:需要 jre 環境!

1.什麼是AWT技術?

AWT(Abstract Window Toolkit)抽象視窗工具集

特點:

  • AWT是重量級元件,因為用了大量的Windows函數
  • AWT隻能在Windows平台下執行
  • 相比于Swing,AWT的代碼較少且執行速度更快

java.awt包的結構:

GUI程式設計核心技術之一(AWT)

2.容器

容器(Container)也是一個類,實際上是Component的子類,是以容器本身也是一個元件,具有元件的所有性質,但是它的主要功能是容納其它元件和容器。容器可以簡化圖形化界面的設計,以整體結構來布置界面。所有的容器都可以通過add()方法向容器中添加元件。

(1)架構Frame

public class TestFrame {
    public static void main(String[] args) {
        //Frame,JDK, 看源碼!
        Frame frame = new Frame("我的第一個Java圖像界面視窗");
        //設定視窗大小
        frame.setSize(400,400);
        //彈出的初始位置
        frame.setLocation(200,200);
        //設定背景顔色  Color
        frame.setBackground(new Color(85, 150, 68));
        //設定大小固定
        frame.setResizable(false);
        //需要設定可見性  
        frame.setVisible(true);
    }
}
           

注意:視窗不能關閉,如果關閉需要關閉程式

(2)面闆Panel

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Panel 可以看成是一個空間,但是不能單獨存在
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        //設定布局
        frame.setLayout(null);//絕對布局
        //坐标
        frame.setBounds(300,300,500,500);   // x,y:視窗位置
        frame.setBackground(new Color(40, 161, 35));
        //panel設定坐标,相對于frame
        panel.setBounds(50,50,400,400); //設定畫闆位置大小
        panel.setBackground(new Color(193, 15, 60));//設定畫闆顔色
        frame.add(panel);     //将畫闆添加到視窗中
        frame.setVisible(true);
        //監聽事件,監聽視窗關閉事件  System.exit(0)
        //擴充卡模式 :
        frame.addWindowListener(new WindowAdapter() {
            //視窗點選關閉的時候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //結束程式
                System.exit(0);
            }
        });
    }
}
           

注意:Panel 無法單獨顯示,必須添加到某個容器中。

3.布局管理器

GUI程式設計核心技術之一(AWT)

(1)流式布局

import java.awt.*;
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //元件-按鈕
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        //設定為流式布局
        //frame.setLayout(new FlowLayout());
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//将三個按鈕放在視窗的最右邊
        frame.setSize(200,200);
        //把按鈕添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.setVisible(true);
    }
}
           

(2)邊界布局-東南西北中

import java.awt.*;
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");
        //設定4個按鈕
        Button east = new Button("East");
        Button west = new Button("West");
        Button south = new Button("South");
        Button north = new Button("North");
        Button center = new Button("Center");
       // 将4個按鈕放到視窗的東南西北中處
        frame.add(east,BorderLayout.EAST);
        frame.add(west,BorderLayout.WEST);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(north,BorderLayout.NORTH);
        frame.add(center,BorderLayout.CENTER);
        frame.setSize(200,200);//視窗尺寸
        frame.setVisible(true);//視窗可見
    } 
}
           

(3)表格式布局

public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        //設定6個按鈕
        Button btn1 = new Button("btn1");
        Button btn2 = new Button("btn2");
        Button btn3 = new Button("btn3");
        Button btn4 = new Button("btn4");
        Button btn5 = new Button("btn5");
        Button btn6 = new Button("btn6");
        //設定表格式布局,參數1行數,參數2列數
        frame.setLayout(new GridLayout(3,2));
        //将6個按鈕添加到視窗中
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);
        frame.pack(); //适應性視窗
        frame.setVisible(true);//視窗可見
    }
}
           

4.事件監聽

(1)視窗監聽:實作視窗的關閉,激活等監聽

public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class WindowFrame extends Frame{
    public WindowFrame(){
        setBackground(Color.blue);
        setBounds(100,100,200,200);
        setVisible(true);
        //addWindowListener(new MyWindowListener());
        this.addWindowListener(
                //匿名内部類
            new WindowAdapter() {
                //關閉視窗
                @Override
                public void windowClosing(WindowEvent e) {
                    System.out.println("windowClosing");
                    System.exit(0);  
                }
                //激活視窗
                @Override
                public void windowActivated(WindowEvent e) {
                    WindowFrame source = (WindowFrame) e.getSource();
                    source.setTitle("被激活了");//被激活後視窗的擡頭
                    System.out.println("windowActivated");
                }
            }
        );
    }
}
           

(2)滑鼠監聽:實作滑鼠畫畫

//滑鼠監聽事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("畫圖");
    }
}
//自己的類
class MyFrame extends Frame{
    //畫畫需要畫筆,需要監聽滑鼠目前的位置,需要集合來存儲這個點
    ArrayList points;
    public MyFrame(String title) {
        super(title);
        setBounds(200,200,400,300);
        //存滑鼠點選的點
        points = new ArrayList<>();
        setVisible(true);
        //滑鼠監聽器,正對這個視窗
        this.addMouseListener(new MyMouseListener());
    }
    @Override
    public void paint(Graphics g) {
        //畫畫,監聽滑鼠的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
           Point point = (Point) iterator.next();
           g.setColor(Color.BLUE);
           g.fillOval(point.x,point.y,10,10);
    }
    }
    //添加一個點到界面上
    public void addPaint(Point point){
        points.add(point);
    }
    //擴充卡模式
    private class MyMouseListener extends MouseAdapter{
        //滑鼠 按下,彈起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame = (MyFrame) e.getSource();
            //這個我們點選的時候,就會在界面上産生一個點!畫
            //這個點就是滑鼠的點;
            frame.addPaint(new Point(e.getX(),e.getY()));
            //每次點選滑鼠都需要重新畫一遍
            frame.repaint();//重新整理   30幀  60幀
        }
    }
}
           

(3)鍵盤監聽:監聽鍵盤輸入的東西

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(1,2,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //鍵盤按下
            @Override
            public void keyPressed(KeyEvent e) {
                //獲得鍵盤下的鍵是哪一個,目前的碼
                int keyCode = e.getKeyCode(); //不需需要去記錄這個數值,直接使用靜态屬性 VK_XXX
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按下了上鍵");
                }
                //根據按下不同操作,産生不同結果;
            }
        });
    }
}
           

(4)輸入框 TextField 監聽

public class TestText01 {
    public static void main(String[] args) {
        //啟動!
        new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);
        //監聽這個文本框輸入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter 就會觸發這個輸入框的事件
        textField.addActionListener(myActionListener2);
        //設定替換編碼
        textField.setEchoChar('*');//輸入後被替換為*
        setVisible(true);
        pack();
    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();     //獲得一些資源,傳回的一個對象
        System.out.println(field.getText()); //獲得輸入框的文本
        field.setText(""); //null  ""
    }
}