Java 基礎(chǔ) 09. Java GUI

GUI 組件:

  • 窗口、彈窗烤芦、面板举娩、文本框、列表框拍棕、按鈕晓铆、圖片勺良、監(jiān)聽事件绰播、鼠標(biāo)、鍵盤事件尚困、破解工具

一蠢箩、簡介

GUI 核心技術(shù):Swing AWT

  • 界面不美觀;
  • 需要 JRE 環(huán)境事甜。

為什么要學(xué)習(xí)谬泌?

  • 可以寫出自己的小工具;
  • 工作時(shí)逻谦,可能會維護(hù)到 Swing 界面掌实;
  • 了解 MVC 架構(gòu),了解監(jiān)聽邦马。

二贱鼻、AWT

1.Awt 簡介(英文縮寫:抽象的窗口工具)

  • 包含了很多類和接口!GUI:圖形用戶界面編程滋将。
  • 元素:窗口邻悬、按鈕、文本框
  • java.awt
  1. 組件和容器
1. Frame
  • 實(shí)例 1:
package com.xxx.gui;

import java.awt.*;

/**
 * GUI 界面
 */
public class TestFrame {
    public static void main(String[] args) {
        // Frame
        Frame frame = new Frame("JAVA 圖形界面");
        // 設(shè)置可見性
        frame.setVisible(true);
        // 設(shè)置窗口大小
        frame.setSize(300, 300);
        // 設(shè)置背景顏色 Color
        frame.setBackground(new Color(220, 218, 218));
        // 彈出的初始位置
        frame.setLocation(300, 200);
        // 設(shè)置大小固定
        frame.setResizable(false);
    }
}
  • 問題:窗口不能閉随闽,停止 Java 程序
  • 實(shí)例 2:調(diào)用封裝類父丰,創(chuàng)建多個(gè)窗口
package com.xxx.gui;

import java.awt.*;

/**
 * 調(diào)用封裝類,創(chuàng)建多個(gè)窗口
 */
public class TestFrame02 {
    public static void main(String[] args) {
        MyFrame myFrame01 = new MyFrame(100, 100, 200, 200, Color.cyan);
        MyFrame myFrame02 = new MyFrame(300, 100, 200, 200, Color.blue);
        MyFrame myFrame03 = new MyFrame(100, 300, 200, 200, Color.gray);
        MyFrame myFrame04 = new MyFrame(300, 300, 200, 200, Color.MAGENTA);
    }
}

class MyFrame extends Frame {
    // 窗口計(jì)數(shù)器
    static int id = 0;

    /**
     * @param x
     * @param y
     * @param width
     * @param height
     * @param color
     */
    public MyFrame(int x, int y, int width, int height, Color color) {
        // 標(biāo)題
        super("MyFrame" + (++id));
        // 坐標(biāo)及大小
        setBounds(x, y, width, height);
        // 背景顏色
        setBackground(color);
        // 設(shè)置可見
        setVisible(true);
    }
}
2. 面板 Panel
  • panel 可以看成一個(gè)單獨(dú)空間掘宪,但不能單獨(dú)存在蛾扇,需要放在 Frame 中
  • 實(shí)例:窗口中添加面板攘烛,并通過事件監(jiān)聽,解決窗口無法關(guān)閉問題
package com.xxx.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

//  panel 可以看成一個(gè)單獨(dú)空間镀首,但不能單獨(dú)存在医寿,需要放在 Frame 中
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        // 布局的概念
        Panel panel = new Panel();
        // 設(shè)置布局
        frame.setLayout(null);
        // 設(shè)置窗口
        // 背景顏色
//        frame.setBackground(Color.LIGHT_GRAY);
        frame.setBackground(new Color(221, 225, 225));
        //坐標(biāo)及尺寸
        frame.setBounds(100, 100, 500, 500);
        // 設(shè)置 panel
        panel.setBackground(new Color(14, 203, 182));
        // 坐標(biāo)為相對坐標(biāo)(相對 Frame)
        panel.setBounds(50, 50, 300, 300);
        // panel 加入到 Frame 中
        frame.add(panel);
        // 設(shè)置可見
        frame.setVisible(true);
        // 監(jiān)聽事件,監(jiān)聽窗口關(guān)閉事件 System.exit(0)
        // 適配器模式
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉蘑斧,需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                // 退出程序
                System.exit(0);
            }
        });
    }
}
3. 布局管理器
  • 流式布局
  • 按鈕 Button button = new Button("button");
  • 實(shí)例:
package com.xxx.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 流式布局
 */
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");
        // 設(shè)置為流式布局(默認(rèn))
        frame.setLayout(new FlowLayout());  // 默認(rèn)居中
//        frame.setLayout(new FlowLayout(FlowLayout.LEFT)); 靠左
//        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));    靠右
        frame.setSize(300, 300);
        // 添加按鈕
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

        // 監(jiān)聽事件:適配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 退出程序
                System.exit(0);
            }
        });
    }
}
  • 東西南北中布局
  • 實(shí)例:
package com.xxx.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 布局:東西南北中
 */
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");

        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");
        // 按布局格式添加
        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(300, 300);
        frame.setVisible(true);
        // 監(jiān)聽事件:適配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
  • 表格布局
  • 實(shí)例:
package com.xxx.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 表格布局
 */
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        // 設(shè)置布局 3行2列
        frame.setLayout(new GridLayout(3, 2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

//        frame.setSize(350, 350);
        // Java 函數(shù)靖秩,自動(dòng)布局
        frame.pack();
        frame.setVisible(true);
        // 監(jiān)聽事件:適配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 退出程序
                System.exit(0);
            }
        });
    }
}
  • 實(shí)例:布局嵌套
package com.xxx.gui;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 布局嵌套
 */
public class ExDemo {
    public static void main(String[] args) {
        // 總 Frame
        Frame frame = new Frame("ExDemo");
        frame.setSize(500, 500);
        frame.setBackground(Color.lightGray);

        // 初始坐標(biāo)位置
        frame.setLocation(300, 200);
        // 可見
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2, 1));
        // 四個(gè)面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new GridLayout(2, 1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2, 2));
        // 上面布局
        p1.add(new Button("EAST-1"), BorderLayout.EAST);
        p1.add(new Button("WEST-1"), BorderLayout.WEST);
        p1.add(p2, BorderLayout.CENTER);
        p2.add(new Button("p2-bnt1"));
        p2.add(new Button("p2-bnt2"));
        // 下面布局
        p3.add(new Button("EAST-2"), BorderLayout.EAST);
        p3.add(new Button("WEST-2"), BorderLayout.WEST);
        p3.add(p4, BorderLayout.CENTER);
        for (int i = 1; i <= 4; i++) {
            p4.add(new Button("p4-for-" + i));
        }

        frame.add(p1);
        frame.add(p3);

        // 自動(dòng)布局
//        frame.pack();
        // 監(jiān)聽事件(窗口關(guān)閉):適配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }
}

總結(jié):

  1. Frame 是一個(gè)頂級窗口;
  2. Panel 無法單獨(dú)顯示竖瘾,必須添加到某個(gè)容器中沟突。
  3. 布局管埋器
    1. 流式布局
    2. 東西南北中布局
    3. 表格布局
    4. 大小,定位捕传,背景顏色惠拭,可見性,監(jiān)聽庸论!
4. 事件監(jiān)聽
  • 事件監(jiān)聽:當(dāng)某個(gè)事情發(fā)生時(shí)职辅,干什么?
  • 實(shí)例 1:
package com.xxx.gui.demo02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 事件監(jiān)聽
 */
public class TestActionEvent {
    public static void main(String[] args) {
        // 按下按鈕聂示,觸發(fā)事件
        Frame frame = new Frame("事件監(jiān)聽");
        Button button = new Button("ActionEvent");
        // 因?yàn)?addActionListener() 需要一個(gè) ActionListener域携,
        // 所以我們需要構(gòu)造一個(gè)ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        // 關(guān)閉窗體
        windowsClose(frame);
    }

    // 關(guān)閉窗體方法
    private static void windowsClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉時(shí)要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }
}

// 事件監(jiān)聽
class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("觸發(fā)事件");
    }
}
  • 實(shí)例 2:多個(gè)按鈕,共享一個(gè)事件
package com.xxx.gui.demo02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 多個(gè)按鈕鱼喉,共享一個(gè)事件
 */
public class TestAction02 {
    public static void main(String[] args) {
        // 兩個(gè)按鈕秀鞭,實(shí)現(xiàn)同一個(gè)監(jiān)聽
        // 開始   停止
        Frame frame = new Frame("開始-停止");
        Button start = new Button("start");
        Button stop = new Button("stop");
        // 可以顯示的定義觸發(fā)會返回的信息,如果不顯示定義扛禽,則為默認(rèn)值锋边!
        // 可以多個(gè)按鈕只寫一個(gè)監(jiān)聽類!
        stop.setActionCommand("but-stop");
        MyMonitor myMonitor = new MyMonitor();
        // 共用一個(gè)事件
        start.addActionListener(myMonitor);
        stop.addActionListener(myMonitor);

        frame.add(start, BorderLayout.NORTH);
        frame.add(stop, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
        windowsClose(frame);
    }

    // 關(guān)閉窗體方法
    private static void windowsClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉時(shí)要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }
}

class MyMonitor implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // e.getActionCommand(); 獲取按鈕信息
//        System.out.println("按鈕被點(diǎn)擊编曼,msg:" + e.getActionCommand());
        if (e.getActionCommand().equals("start")) {
            System.out.println("開始按鈕被點(diǎn)擊");
        } else {
            System.out.println("停止按鈕被點(diǎn)擊");
        }
    }
}
5. 輸入框(TextField)監(jiān)聽事件
  • 實(shí)例:
package com.xxx.gui.demo02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 輸入框(TextField)監(jiān)聽事件
 */
public class TestTextField {
    public static void main(String[] args) {
        // 啟動(dòng)
//        MyFrame myFrame = new MyFrame();
//        windowsClose(myFrame);
        windowsClose(new MyFrame());
    }
    // 關(guān)閉窗體方法
    private static void windowsClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉時(shí)要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }
}

class MyFrame extends Frame {
    public MyFrame() {
        TextField textField = new TextField();
        add(textField);
        // 監(jiān)聽文本框的輸入
        MyActionListener2 myActionListener2 = new MyActionListener2();
        // 按下回車豆巨,觸發(fā)輸入框事件
        textField.addActionListener(myActionListener2);
        // 設(shè)置替換編碼
        textField.setEchoChar('*');
        pack();
        setVisible(true);
    }
}
// 文本框監(jiān)聽
class MyActionListener2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        // 獲得資源返回的對象(Object)
        TextField field = (TextField) e.getSource();    // 類型轉(zhuǎn)換
        // 獲得輸入框中的文本
        System.out.println(field.getText());
        // 回車后,清空文本框
        field.setText("");
    }
}
6. 簡易計(jì)算器:組合+內(nèi)部類回顧
  • OOP 原則:組合掐场,大于繼承往扔!
// 繼承
class A extends B{
}
// 組合
class A{
    public B b;
}
  • 實(shí)例 1:簡易計(jì)算器
package com.xxx.gui.demo02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 簡易計(jì)算器
 */
public class TestCalc {
    public static void main(String[] args) {
        windowsClose(new Calculator());
    }

    // 關(guān)閉窗體方法
    private static void windowsClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉時(shí)要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }
}

// 計(jì)算器類
class Calculator extends Frame {
    public Calculator() {
        // 3 個(gè)文本框
        TextField num1 = new TextField(10); // 10 字符數(shù)
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);
        // 1 個(gè)按鈕
        Button button = new Button("=");
        // 按鈕添加監(jiān)聽
        button.addActionListener(new MyCalculatorListener(num1, num2, num3));
        // 1 個(gè)標(biāo)簽
        Label label = new Label("+");
        // 設(shè)置布局(流式)
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}

// 監(jiān)聽器類
class MyCalculatorListener implements ActionListener {
    // 獲取三個(gè)變量
    private TextField num1, num2, num3;

    public MyCalculatorListener(TextField num1, TextField num2, TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 1.獲取加數(shù)和被加數(shù)
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        // 2.將兩個(gè)值 + 法運(yùn)算后,放到第三個(gè)框
        num3.setText("" + (n1 + n2));   // 轉(zhuǎn)成字符串類
        // 3.清除前兩個(gè)框
        num1.setText("");
        num2.setText("");
    }
}
  • 實(shí)例 2:改為面向?qū)ο?/li>
package com.xxx.gui.demo02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 簡易計(jì)算器
 */
public class TestCalc {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

// 計(jì)算器類
class Calculator extends Frame {
    // 屬性
    TextField num1, num2, num3;

    // 方法
    public void loadFrame() {
        // 3 個(gè)文本框刻肄、1 個(gè)按鈕瓤球、1 個(gè)標(biāo)簽
        num1 = new TextField(10); // 10 字符數(shù)
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        // 按鈕添加監(jiān)聽
        button.addActionListener(new MyCalculatorListener(this));

        // 設(shè)置布局(流式)
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
}

// 監(jiān)聽器類
class MyCalculatorListener implements ActionListener {
    // 獲取計(jì)算器對象(一個(gè)類中組合另一個(gè)類)
    Calculator calculator = null;

    public MyCalculatorListener(Calculator calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 1.獲取加數(shù)和被加數(shù)
        // 2.將兩個(gè)值 + 法運(yùn)算后,放到第三個(gè)框
        // 3.清除前兩個(gè)框
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        calculator.num3.setText("" + (n1 + n2));   // 轉(zhuǎn)成字符串類
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}
  • 實(shí)例 3:內(nèi)部類(更好的方式)
  • 好處:就是可以暢通無阻的訪問外部屬性和方法敏弃!
package com.xxx.gui.demo02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 簡易計(jì)算器
 */
public class TestCalc {
    public static void main(String[] args) {
        new Calculator("簡易計(jì)算器").loadFrame();
    }
}

// 計(jì)算器類
class Calculator extends Frame {
    // 屬性
    TextField num1, num2, num3;

    // 方法
    public void loadFrame() {
        // 3 個(gè)文本框卦羡、1 個(gè)按鈕、1 個(gè)標(biāo)簽
        num1 = new TextField(10); // 10 字符數(shù)
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        // 按鈕添加監(jiān)聽
        button.addActionListener(new MyCalculatorListener());

        // 設(shè)置布局(流式)
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
        windowsClose(this);
    }

    // 監(jiān)聽器類(內(nèi)部類方式)
    // 內(nèi)部類最大的好處,就是可以暢通無阻的訪問外部屬性和方法绿饵!
    private class MyCalculatorListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 1.獲取加數(shù)和被加數(shù)
            // 2.將兩個(gè)值 + 法運(yùn)算后欠肾,放到第三個(gè)框
            // 3.清除前兩個(gè)框
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            num3.setText("" + (n1 + n2));   // 轉(zhuǎn)成字符串類
            num1.setText("");
            num2.setText("");
        }
    }

    // 關(guān)閉窗體方法
    private static void windowsClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉時(shí)要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }

    public Calculator(String title) {
        super(title);
    }
}
7. 畫筆
  • 實(shí)例:
package com.xxx.gui.demo03;

import java.awt.*;

/**
 * 畫筆
 */
public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().loadFrame();
    }
}

// 窗口類
class MyPaint extends Frame {
    public void loadFrame() {
        setBounds(200, 200, 500, 400);
        setVisible(true);
    }

    // 重寫畫筆方法
    @Override
    public void paint(Graphics g) {
        // 畫筆顏色
        g.setColor(Color.RED);
        // 空心圓:起始坐標(biāo),長度拟赊,寬度
//        g.drawOval(100, 100, 150, 150);
        g.fillOval(50, 50, 120, 120);   // 實(shí)心圓
        g.setColor(Color.GREEN);
        // 矩形
        g.fillRect(160, 160, 150, 150);
        // 養(yǎng)成習(xí)慣刺桃,畫筆用完,恢復(fù)為初始顏色(黑色)
        g.setColor(Color.BLACK);
    }
}

鼠標(biāo)監(jiān)聽

  • 實(shí)例:實(shí)現(xiàn)用鼠標(biāo)畫畫吸祟。
package com.xxx.gui.demo03;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 鼠標(biāo)監(jiān)聽:實(shí)現(xiàn)用鼠標(biāo)畫畫
 * 1.鼠標(biāo)監(jiān)聽:獲取坐標(biāo)
 * 2.將坐標(biāo)存儲到集合
 * 3.重寫畫筆方法瑟慈,遍歷每個(gè)坐標(biāo)點(diǎn),并畫出
 */
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("畫圖");
    }
}

// 自定義類
class MyFrame extends Frame {
    // 需要畫筆屋匕,以及監(jiān)聽鼠標(biāo)當(dāng)前位置葛碧,使用集合來存儲每一個(gè)點(diǎn)
    ArrayList points;

    // 窗口:構(gòu)造方法
    public MyFrame(String title) {
        super(title);
        setBounds(200, 200, 500, 400);
        setVisible(true);
        windowsClose(this);

        // 2.存:鼠標(biāo)點(diǎn)擊的坐標(biāo)點(diǎn),并提到方法外
        points = new ArrayList<>();
        // 鼠標(biāo)監(jiān)聽器:正對這個(gè)窗口
        this.addMouseListener(new MyMouseListener());
    }

    // 重寫畫筆方法
    @Override
    public void paint(Graphics g) {
        // 3.畫:監(jiān)聽鼠標(biāo)事件(遍歷存儲到集合中的鼠標(biāo)事件坐標(biāo)點(diǎn))
        Iterator iterator = points.iterator();
        while (iterator.hasNext()) {
            Point point = (Point) iterator.next();
            g.setColor(Color.BLUE);
            g.fillOval(point.x, point.y, 10, 10);
        }
    }

    // 2.自定義方法:添加一個(gè)點(diǎn)到集合 ArrayList points
    public void addPaint(Point point) {
        points.add(point);
    }

    // 鼠標(biāo)監(jiān)聽:適配器模式
    private class MyMouseListener extends MouseAdapter {
        // 鼠標(biāo):按下过吻、彈起进泼、按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            // 獲取鼠標(biāo)資源(Object)
            MyFrame myFrame = (MyFrame) e.getSource();  // 轉(zhuǎn)換成 MyFrame 類
            // 1.獲取:鼠標(biāo)點(diǎn)擊纤虽,就會在界面上產(chǎn)生一個(gè)點(diǎn)(鼠標(biāo)坐標(biāo))
            // new Point(e.getX(), e.getY())
            // 通過自定義方法 addPaint()乳绕,將坐標(biāo)存儲添加到集合中
            myFrame.addPaint(new Point(e.getX(), e.getY()));
            // 每點(diǎn)擊鼠標(biāo),重畫一次
            myFrame.repaint();  // 刷新(幀)
        }
    }

    // 關(guān)閉窗體方法
    private static void windowsClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            // 窗口點(diǎn)擊關(guān)閉時(shí)要做的事
            @Override
            public void windowClosing(WindowEvent e) {
                // 結(jié)束程序
                System.exit(0);
            }
        });
    }
}

窗口監(jiān)聽

  • 實(shí)例:
package com.xxx.gui.demo03;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗口監(jiān)聽
 */
public class TestWindow {
    public static void main(String[] args) {
        new WindowFrame("窗口監(jiān)聽");
    }
}

// 自定義類
class WindowFrame extends Frame {
    public WindowFrame(String title) {
        super(title);
        setBackground(new Color(141, 234, 233));
        setBounds(100, 100, 300, 300);
        setVisible(true);
//        // 內(nèi)部類方式(窗口關(guān)閉)
//        addWindowListener(new MyWindowListener());

        // 推薦:匿名內(nèi)部類方式
        this.addWindowListener(new WindowAdapter() {
            // 窗口關(guān)閉
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

            // 窗口激活
            @Override
            public void windowActivated(WindowEvent e) {
                WindowFrame source = (WindowFrame) e.getSource();
                source.setTitle("窗口被激活");
            }
        });
    }

    // 內(nèi)部類方式(窗口關(guān)閉)
//    class MyWindowListener extends WindowAdapter {
//        @Override
//        public void windowClosing(WindowEvent e) {
//            setVisible(false);  // 窗口隱藏
//            System.exit(0); // 正常退出逼纸, 1 為非正常退出
//        }
//    }
}

鍵盤監(jiān)聽

  • 實(shí)例:
package com.xxx.gui.demo03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 鍵盤監(jiān)聽
 */
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(100, 100, 300, 300);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            // 鍵盤按下
            @Override
            public void keyPressed(KeyEvent e) {
                // 獲取鍵盤按下洋措,當(dāng)前鍵的碼
                int keyCode = e.getKeyCode();
                // 可以直接使用靜態(tài)屬性
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("按下了向上鍵");
                }
                System.out.println(keyCode);
                // 根據(jù)按下的鍵不同,產(chǎn)生不同的結(jié)果
            }
        });
    }
}

三樊展、Swing

  1. 窗口呻纹、面板
package com.xxx.gui.demo04;

import javax.swing.*;
import java.awt.*;

/**
 * Swing 窗口、面板
 */
public class JFrameDemo {
    public static void main(String[] args) {
        // 建立一個(gè)窗口
        new MyJFrame().init();
    }
}

class MyJFrame extends JFrame {
    // init(); 初始化
    public void init() {
        // 頂級窗口
        this.setBounds(100, 100, 300, 300);
        this.setVisible(true);
        JLabel jLabel = new JLabel("JLabel");
        // 文本標(biāo)簽居中专缠,設(shè)置水平對齊
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);
        this.add(jLabel);

        // 獲得一個(gè)容器(顏色必須在容器中設(shè)置)
        Container container = this.getContentPane();
        container.setBackground(Color.CYAN);
        // 關(guān)閉事件
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

2.JDialog 彈窗

  • 用來被彈出,默認(rèn)自帶關(guān)閉事件
  • 實(shí)例:
package com.xxx.gui.demo04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * JDialog 彈窗
 */
// 主窗口
public class DialogDemo extends JFrame {
    public DialogDemo() {
        this.setSize(500, 300);
        this.setVisible(true);
        // 關(guān)閉
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        // JFrame 容器:放置組件
        Container container = this.getContentPane();
        // 絕對布局
        container.setLayout(null);
        // 按鈕
        JButton button = new JButton("點(diǎn)擊彈出對話框");
        // 位置相對于 JFrame 容器
        button.setBounds(30, 30, 200, 50);
        // 按鈕監(jiān)聽器:點(diǎn)擊彈出窗口
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 彈窗
                new MyDialogDemo();
            }
        });
        container.add(button);
    }

    public static void main(String[] args) {
        new DialogDemo();
    }
}
// 彈窗窗口
class MyDialogDemo extends JDialog {
    public MyDialogDemo() {
        this.setBounds(300, 200, 300, 200);
        this.setVisible(true);
        // 報(bào)錯(cuò)(重復(fù)):JDialog 默認(rèn)已包含
        // this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container container = this.getContentPane();
        container.setLayout(null);
        JLabel label = new JLabel("彈窗窗口");
        label.setBounds(10, 10, 50, 50);
        container.add(label);
    }
}
  1. 標(biāo)簽
  • 實(shí)例 1:圖標(biāo)
package com.xxx.gui.demo04;

import javax.swing.*;
import java.awt.*;

/**
 * 標(biāo)簽:Icon
 */
// 圖標(biāo)(接口)淑仆,需要實(shí)現(xiàn)類涝婉,繼承 JFrame
public class IconDemo extends JFrame implements Icon {
    private int width;
    private int height;

    // 無參構(gòu)造
    public IconDemo() {
    }

    // 帶參構(gòu)造
    public IconDemo(int width, int height) {
        this.width = width;
        this.height = height;
    }

    // 初始化
    public void init() {
        IconDemo iconDemo = new IconDemo(15, 15);
        // 圖標(biāo)可以放在標(biāo)簽,也可以放在按鈕上
        JLabel label = new JLabel("iconTest", iconDemo, SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        this.setVisible(true);
        this.pack();
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new IconDemo().init();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x, y, width, height);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }
}
  • 實(shí)例 2:圖片
package com.xxx.gui.demo04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * 標(biāo)簽:圖片
 */
public class ImageIconDemo extends JFrame {
    public ImageIconDemo() {
        JLabel label = new JLabel("ImageIcon");
        // 獲取圖片地址
        URL url = ImageIconDemo.class.getResource("logo.png");
        ImageIcon imageIcon = new ImageIcon(url);
        // 標(biāo)簽設(shè)置圖片
        label.setIcon(imageIcon);
        // 居中
        label.setHorizontalAlignment(SwingConstants.CENTER);
        Container container = getContentPane();
        container.add(label);
        setVisible(true);
        pack();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ImageIconDemo();
    }
}
  1. 面板:JPanel
  • 實(shí)例:
package com.xxx.gui.demo05;

import javax.swing.*;
import java.awt.*;

/**
 * 面板:JPanel
 */
public class JPanelDemo extends JFrame {

    public JPanelDemo() {
        Container container = this.getContentPane();
        // 表格布局:兩行一列蔗怠,間距都為10
        container.setLayout(new GridLayout(2, 1, 10, 10));

        JPanel panel1 = new JPanel(new GridLayout(1, 3));
        JPanel panel2 = new JPanel(new GridLayout(1, 2));
        JPanel panel3 = new JPanel(new GridLayout(2, 1));
        JPanel panel4 = new JPanel(new GridLayout(2, 2));
        for (int i = 0; i < 3; i++) {
            panel1.add(new JButton("1"));
        }
        for (int i = 0; i < 2; i++) {
            panel2.add(new JButton("2"));
        }
        for (int i = 0; i < 2; i++) {
            panel3.add(new JButton("3"));
        }
        for (int i = 0; i < 6; i++) {
            panel4.add(new JButton("4"));
        }
        container.add(panel1);
        container.add(panel2);
        container.add(panel3);
        container.add(panel4);

        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JPanelDemo();
    }
}
  1. 邊框:JScroll
  • 實(shí)例:
package com.xxx.gui.demo05;

import javax.swing.*;
import java.awt.*;

/**
 * 邊框:JScroll
 */
public class JScrollDemo extends JFrame {
    public JScrollDemo() {
        Container container = this.getContentPane();
        // 文本域(可換行)
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("這是文本域6胀洹!");
        // JScroll 面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JScrollDemo();
    }
}
  1. 按鈕
  • 實(shí)例:圖標(biāo)按鈕
package com.xxx.gui.demo05;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

/**
 * 按鈕: 圖標(biāo)按鈕
 */
public class JButtonDemo extends JFrame {
    public JButtonDemo() {
        Container container = this.getContentPane();
        // 獲取圖片地址
        URL url = JButtonDemo.class.getResource("logo.png");
        // 把圖片轉(zhuǎn)換為圖標(biāo)
        Icon icon = new ImageIcon(url);
        // 把圖標(biāo)放在按鈕上
        JButton button = new JButton();
        button.setIcon(icon);
        // 提示文字
        button.setToolTipText("提示文字:圖片按鈕");
        // 按鈕添加到容器中
        container.add(button);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo();
    }
}
  • 實(shí)例:單選框
package com.xxx.gui.demo05;

import javax.swing.*;
import java.awt.*;

/**
 * 單選框
 */
public class JButtonDemo02 extends JFrame {
    public JButtonDemo02() {
        Container container = this.getContentPane();
        // 單選框
        JRadioButton radioButton1 = new JRadioButton("JRadioButton01");
        JRadioButton radioButton2 = new JRadioButton("JRadioButton02");
        JRadioButton radioButton3 = new JRadioButton("JRadioButton03");
        // 單選框只能選擇一個(gè)寞射,需要將其分成一組渔工,每組中只能選擇一個(gè)
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);

        container.add(radioButton1, BorderLayout.NORTH);
        container.add(radioButton2, BorderLayout.CENTER);
        container.add(radioButton3, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}
  • 實(shí)例:多選框
package com.xxx.gui.demo05;

import javax.swing.*;
import java.awt.*;

/**
 * 多選按鈕
 */
public class JButtonDemo03 extends JFrame {
    public JButtonDemo03() {
        Container container = this.getContentPane();
        // 多選框
        JCheckBox checkBox1 = new JCheckBox("checkBox01");
        JCheckBox checkBox2 = new JCheckBox("checkBox02");

        container.add(checkBox1, BorderLayout.NORTH);
        container.add(checkBox2, BorderLayout.SOUTH);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new JButtonDemo03();
    }
}
  1. 列表
  • 實(shí)例:下拉框
package com.xxx.gui.demo06;

import javax.swing.*;
import java.awt.*;

/**
 * 列表:下拉框
 */
public class ComboboxDemo extends JFrame {
    public ComboboxDemo() {
        Container container = this.getContentPane();
        // 下拉框
        JComboBox status = new JComboBox();
        status.addItem(null);
        status.addItem("下拉內(nèi)容1");
        status.addItem("下拉內(nèi)容2");
        status.addItem("下拉內(nèi)容3");
        container.add(status);
        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ComboboxDemo();
    }
}
  • 實(shí)例:列表框
package com.xxx.gui.demo06;

import javax.swing.*;
import java.awt.*;
import java.util.Vector;

/**
 * 列表框
 */
public class ComboboxDemo02 extends JFrame {
    public ComboboxDemo02() {
        Container container = this.getContentPane();
        // 列表框
        // 生成列表的內(nèi)容
//        String[] contents = {"1", "2", "3"};  數(shù)組
        // 集合方式,可以動(dòng)態(tài)添加列表內(nèi)容
        Vector contents = new Vector();

        JList jList = new JList(contents);
        contents.add("列表內(nèi)容1");
        contents.add("列表內(nèi)容2");
        contents.add("列表內(nèi)容3");
        container.add(jList);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new ComboboxDemo02();
    }
}
  • 應(yīng)用場景
    • 選擇地區(qū)桥温,或者一些單個(gè)選項(xiàng)引矩;
    • 列表:展示信息,一般是動(dòng)態(tài)擴(kuò)容!
  1. 文本框
  • 實(shí)例:文本框
package com.xxx.gui.demo06;

import javax.swing.*;
import java.awt.*;

/**
 * 文本框
 */
public class TextDemo01 extends JFrame {
    public TextDemo01() {
        Container container = this.getContentPane();
        // 文本框
        JTextField textField1 = new JTextField("hello");
        JTextField textField2 = new JTextField("world", 20);
        container.add(textField1, BorderLayout.NORTH);
        container.add(textField2, BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TextDemo01();
    }
}
  • 實(shí)例:密碼框
package github.GUI.Demo06;
import javax.swing.*;
import java.awt.*;
/**
 * @author subeiLY
 * @create 2021-06-05 16:37
 */
// 密碼框
public class TestTextDemo02 extends JFrame {
    public TestTextDemo02(){
        Container container = this.getContentPane();
        JPasswordField passwordField = new JPasswordField();
        passwordField.setEchoChar('*');
        container.add(passwordField);
        this.setVisible(true);
        this.setSize(500,500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new TestTextDemo02();
    }
}
  • 實(shí)例:文本域
package com.xxx.gui.demo06;

import javax.swing.*;
import java.awt.*;

/**
 * 文本域
 */
public class TextDemo03 extends JFrame {
    public TextDemo03() {
        Container container = this.getContentPane();
        // 文本域(可換行)
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("這是文本域M隆氛谜!");
        // JScroll 面板
        JScrollPane scrollPane = new JScrollPane(textArea);
        container.add(scrollPane);

        this.setVisible(true);
        this.setBounds(100, 100, 300, 200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TextDemo03();
    }
}

綜合實(shí)例:貪吃蛇游戲

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市区端,隨后出現(xiàn)的幾起案子值漫,更是在濱河造成了極大的恐慌,老刑警劉巖织盼,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杨何,死亡現(xiàn)場離奇詭異,居然都是意外死亡沥邻,警方通過查閱死者的電腦和手機(jī)晚吞,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谋国,“玉大人槽地,你說我怎么就攤上這事÷” “怎么了捌蚊?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長近弟。 經(jīng)常有香客問我缅糟,道長,這世上最難降的妖魔是什么祷愉? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任窗宦,我火速辦了婚禮,結(jié)果婚禮上二鳄,老公的妹妹穿的比我還像新娘赴涵。我一直安慰自己,他們只是感情好订讼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布髓窜。 她就那樣靜靜地躺著,像睡著了一般欺殿。 火紅的嫁衣襯著肌膚如雪寄纵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天脖苏,我揣著相機(jī)與錄音程拭,去河邊找鬼。 笑死棍潘,一個(gè)胖子當(dāng)著我的面吹牛恃鞋,可吹牛的內(nèi)容都是我干的崖媚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼山宾,長吁一口氣:“原來是場噩夢啊……” “哼至扰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起资锰,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤敢课,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后绷杜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體直秆,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年鞭盟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了圾结。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡齿诉,死狀恐怖筝野,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情粤剧,我是刑警寧澤歇竟,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站抵恋,受9級特大地震影響焕议,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜弧关,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一盅安、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧世囊,春花似錦别瞭、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至号胚,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間浸遗,已是汗流浹背猫胁。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留跛锌,地道東北人弃秆。 一個(gè)月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓届惋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親菠赚。 傳聞我的和親對象是個(gè)殘疾皇子脑豹,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容