https://www.bilibili.com/video/BV1DJ411B75F/

简介

图形用户界面 Graphical User Interface,简称 GUI,又称图形用户接口

GUI核心技术:Swing AWT

不再流行的原因

  1. 界面不美观
  2. 需要jre环境

为什么学习

  1. 可以写小工具。
  2. 有可能需要维护swing界面,概率极小。
  3. 了解MVC架构,了解监听。

AWT

AWT(Abstract Window Toolkit),抽象窗口工具包

  1. 包含了很多类和接口。
  2. 元素:窗口,按钮,文本框。
  3. java.awt

image.png

组件和容器

  1. Frame是一个顶级窗口。
  2. Panel无法单独显示,不许添加到某个容器中。
  3. 布局管理器
    1. 流式布局
    2. 东西南北中
    3. 表格布局

Frame窗口

package com.qing.demo01;

import java.awt.*;

//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        
        //Frame 看源码学习
        Frame frame = new Frame("我的第一个Java图形界面窗口");
        //设置可见性
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //设置背景颜色
        frame.setBackground(new Color(144, 97, 79));
        //弹出的初始位置
        frame.setLocation(200,200);
        //设置大小固定
        frame.setResizable(false);
    }
}

image.png
回顾封装,展示多窗口

package com.qing.demo01;

import java.awt.*;

public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100, 100, 300, 200, Color.red);
        MyFrame myFrame2 = new MyFrame(400, 100, 300, 200, Color.orange);
        MyFrame myFrame3 = new MyFrame(100, 300, 300, 200, Color.yellow);
        MyFrame myFrame4 = new MyFrame(400, 300, 300, 200, Color.green);
    }
}

//定义MyFrame,继承Frame
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);
    }
}

image.png

Panel面板

package com.qing.demo01;

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(200, 200, 500, 500);
        frame.setBackground(new Color(54, 207, 52));
        //panel设置坐标,相对于frame
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(244, 41, 81));
        //frame添加panel
        frame.add(panel);
        //设置可见性
        frame.setVisible(true);

        //监听事件,监听窗口关闭事件
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            //使用匿名内部类
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序:0正常结束;非0异常结束,如1
                System.exit(0);
            }
        });
    }
}

image.png

布局管理器

  1. 流式布局:自左而右,自上而下。
package com.qing.demo01;

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.setSize(400, 400);
        frame.setVisible(true);

        //把按钮添加到Frame
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
    }
}

image.png

  1. 东西南北中
package com.qing.demo01;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //组件-按钮
        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(400, 400);
        frame.setVisible(true);
    }
}

image.png

  1. 表格布局
package com.qing.demo01;

import java.awt.*;

public class TestGridLayout {
    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");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");

        //表格布局,2行3列
        frame.setLayout(new GridLayout(2, 3));

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);

//        frame.setSize(400, 400);
        frame.pack();//自适应
        frame.setVisible(true);
    }
}

image.png

  1. 组合布局
package com.qing.demo01;

import java.awt.*;

public class TestDemo {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(200,200);
        frame.setBackground(new Color(134,205,90));
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));

        //四个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new BorderLayout());
        Panel p3 = new Panel(new GridLayout(2,1));
        Panel p4 = new Panel(new GridLayout(2,2));

        //添加按钮到面板
        p1.add(new Button("p1-east"),BorderLayout.EAST);
        p1.add(new Button("p1-west"),BorderLayout.WEST);
        p2.add(new Button("p2-east"),BorderLayout.EAST);
        p2.add(new Button("p2-west"),BorderLayout.WEST);
        p3.add(new Button("p1-center-p3-1"));
        p3.add(new Button("p1-center-p3-2"));
        p4.add(new Button("p2-center-p4-1"));
        p4.add(new Button("p2-center-p4-2"));
        p4.add(new Button("p2-center-p4-3"));
        p4.add(new Button("p2-center-p4-4"));
        //添加面板到面板
        p1.add(p3,BorderLayout.CENTER);
        p2.add(p4,BorderLayout.CENTER);
        //添加面板到frame窗口
        frame.add(p1);
        frame.add(p2);
    }
}

image.png

事件监听

package com.qing.demo02;

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

public class TestActionEvent {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Button button = new Button("big");
        //给按钮添加动作监听
        button.addActionListener(new MyActionListener());

        frame.add(button,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        windowClose(frame);
    }
    //关闭窗体的事件
    private static void windowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
//事件监听
class MyActionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("点击事件");
    }
}

image.png
多个按钮,共享一个事件监听

package com.qing.demo02;

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

public class TestActionTwo {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始-停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        //可以显式的定义触发会返回的命令,如果不显式定义,则会返回默认的值label。
        //可以多个按钮只写一个监听类,通过定义的命令判断是哪个按钮
        button2.setActionCommand("button2-stop");

        MyMonitor myMonitor = new MyMonitor();

        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        windowClose(frame);
    }
    //关闭窗体的事件
    private static void windowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyMonitor implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮的信息:" + e.getActionCommand());
    }
}

image.png

输入框监听

package com.qing.demo02;

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

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();
        //回车就会触发事件
        textField.addActionListener(myActionListener2);
        //输入文字显示为*,输入密码时使用
//        textField.setEchoChar('*');

        setVisible(true);
        pack();
    }
}

class MyActionListener2 implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField textField = (TextField) e.getSource();//获取资源,强转为文本框对象
        System.out.println(textField.getText());
        textField.setText("");//清空输入
    }
}

image.pngimage.png

简易计算器

package com.qing.demo02;

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

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        //启动
        new Calculator();
    }
}

//计算器类
class Calculator extends Frame {
    public Calculator() {
        //3个文本框
        TextField num1 = new TextField(10);//字符数
        TextField num2 = new TextField(10);//字符数
        TextField num3 = new TextField(10);//字符数
        //1个按钮
        Button button = new Button("=");
        button.addActionListener(new MyActionListener3(num1,num2,num3));
        //1个标签
        Label label = new Label("+");
        //布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}

//监听器类
class MyActionListener3 implements ActionListener {
    //获取3个文本框变量
    private TextField num1,num2,num3;

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

    public MyActionListener3() {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int i1 = Integer.parseInt(num1.getText());
        int i2 = Integer.parseInt(num2.getText());
        //2.加法运算后,填入第三个文本框
        num3.setText(String.valueOf(i1 + i2));
        //3.清楚加数和被加数文本框
        num1.setText("");
        num2.setText("");
    }
}

image.png

使用组合优化计算器

组合:在一个类中组合另外一个类

package com.qing.demo02;

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

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        //启动
        new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame {
    //属性
    TextField num1,num2,num3;

    //方法
    public void loadFrame() {
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);//字符数
        num3 = new TextField(10);//字符数
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyActionListener3(this));

        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}

//监听器类
class MyActionListener3 implements ActionListener {

    //组合:在一个类中组合另外一个类
   Calculator calculator;

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

    public MyActionListener3() {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int i1 = Integer.parseInt(calculator.num1.getText());
        int i2 = Integer.parseInt(calculator.num2.getText());
        //2.加法运算后,填入第三个文本框
        calculator.num3.setText(String.valueOf(i1 + i2));
        //3.清楚加数和被加数文本框
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}

使用内部类优化计算器

内部类优点

  1. 更好的封装。
  2. 可以访问外部类的属性和方法。
package com.qing.demo02;

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

//简易计算器
public class TestCalc {
    public static void main(String[] args) {
        //启动
        new Calculator().loadFrame();
    }
}

//计算器类
class Calculator extends Frame {
    //属性
    TextField num1,num2,num3;

    //方法
    public void loadFrame() {
        num1 = new TextField(10);//字符数
        num2 = new TextField(10);//字符数
        num3 = new TextField(10);//字符数
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyActionListener3());

        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

    //监听器类
    //内部类最大的好处就是可以访问外部类的属性和方法。
    private class MyActionListener3 implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            int i1 = Integer.parseInt(num1.getText());
            int i2 = Integer.parseInt(num2.getText());
            //2.加法运算后,填入第三个文本框
            num3.setText(String.valueOf(i1 + i2));
            //3.清楚加数和被加数文本框
            num1.setText("");
            num2.setText("");
        }
    }
}

画笔

package com.qing.demo03;

import javafx.scene.paint.Paint;

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,600,400);
        setVisible(true);
    }
    //画笔
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(100,100,200,200);

        g.setColor(Color.green);
        g.fillRect(150,150,100,100);
    }
}

image.png

鼠标监听

package com.qing.demo03;

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

//鼠标监听
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

class MyFrame extends Frame {
    //窗口,画笔,鼠标点击坐标集合,鼠标监听获取坐标
    //坐标集合
    ArrayList<Point> points;
    public MyFrame(String title) {
        super(title);
        setBounds(200,200,400,300);
        points = new ArrayList();
        setVisible(true);
        //鼠标监听,每次点击,将坐标放入集合,重画,将集合中的坐标重画一次
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                points.add(new Point(e.getX(),e.getY()));
                repaint();
            }
        });
    }

    @Override
    public void paint(Graphics g) {
        Iterator<Point> iterator = points.iterator();
        while (iterator.hasNext()) {
            Point point = iterator.next();
            g.setColor(Color.GREEN);
            g.fillOval(point.x,point.y,20,20);
        }
    }
}

image.png

窗口监听

package com.qing.demo03;

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

public class TestWindowListener {
    public static void main(String[] args) {
        new WindowFrame();
    }
}

class WindowFrame extends Frame {
    int count = 0;
    public WindowFrame() {
        setBackground(new Color(159, 193, 84));
        setBounds(200,200,400,300);
        setVisible(true);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                System.exit(0);
            }

            @Override
            public void windowActivated(WindowEvent e) {
                System.out.println("windowActivated");
                setTitle("激活窗口" + (++count) + "次");
            }
        });
    }
}

image.png

键盘监听

package com.qing.demo03;

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

public class TestKeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(200,200,400,300);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                System.out.println(keyCode);
                setTitle(String.valueOf(keyCode));
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("上键");
                }
            }
        });
    }
}

image.png

Swing

窗口,面板

package com.qing.demo04;

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

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

class MyJframe extends JFrame {
    public void init() {
        setBounds(200,200,400,300);
        setVisible(true);
        //关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JLabel label = new JLabel("欢迎来到武侠世界!");
        add(label);
        //标签居中
        label.setHorizontalAlignment(SwingConstants.CENTER);

        //获得一个容器
        Container container = getContentPane();
        container.setBackground(Color.ORANGE);

    }
}

image.png

弹窗

package com.qing.demo04;

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

public class JDialogDemo extends JFrame {
    public JDialogDemo() {
        setVisible(true);
        setBounds(200,200,400,400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = getContentPane();
        //绝对布局
        container.setLayout(null);

        JButton button = new JButton("点击送秘籍");
        button.setBounds(30,30,200,50);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyJdialog();
            }
        });
        container.add(button);
    }

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

class MyJdialog extends JDialog {
    public MyJdialog() {
        setVisible(true);
        setBounds(300,300,150,250);
        setTitle("九阳真经");

        Container container = getContentPane();
        //绝对布局
        container.setLayout(null);
    }
}

image.png

标签,图标,图片图标

package com.qing.demo04;

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

public class IconDemo extends JFrame implements Icon {

    private int width;
    private int height;

    public IconDemo() {
    }

    public IconDemo(int width, int height) {
        this.width = width;
        this.height = height;
    }

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

    public void init() {
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,400,300);

        Container container = getContentPane();

        IconDemo icon = new IconDemo(15,15);
        //图标放在标签,也可以放在按钮上
        JLabel label = new JLabel("icon",icon,SwingConstants.CENTER);
        container.add(label);
    }

    @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;
    }
}

image.png

package com.qing.demo04;

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

public class ImageIconDemo extends JFrame {

    public ImageIconDemo(){
        setVisible(true);
        setBounds(200,200,400,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = getContentPane();

        //获取当前类路径下的图片
        URL url = ImageIconDemo.class.getResource("qing.jpg");
        ImageIcon imageIcon = new ImageIcon(url);
        JLabel label = new JLabel();
        label.setIcon(imageIcon);
        label.setHorizontalAlignment(SwingConstants.CENTER);

        container.add(label);
    }

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

image.png

面板

package com.qing.demo05;

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

public class JPanelDemo extends JFrame {
    public JPanelDemo() {
        setVisible(true);
        setBounds(200,200,400,300);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        Container container = getContentPane();
        //后面的参数是间距
        container.setLayout(new GridLayout(2,1,10,10));

        JPanel panel1 = new JPanel(new GridLayout(1,3,15,15));
        JPanel panel2 = new JPanel(new GridLayout(2,2,5,5));

        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel1.add(new JButton("1"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));
        panel2.add(new JButton("2"));

        container.add(panel1);
        container.add(panel2);
    }

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

image.png

按钮

  1. 普通按钮
  2. 单选按钮 new JRadioButton(); 分组 new ButtonGroup();
  3. 多选按钮 new JCheckBox();

列表

  1. 下拉框 new JCombobox();
  2. 列表 new JList();

文本框

  1. 文本框 new JTextField();
  2. 密码框 new JPasswordField();
  3. 文本域 new JTextArea();

GUI总结

版权声明:本文为wl3pb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wl3pb/p/13971639.html