Java基础编程-GUI编程

本文全文参考 https://www.bilibili.com/video/BV1DJ411B75F

1.简介

  • GUI的核心技术:Swing、AWT
    • 缺点:界面不美观、需要JRE环境
  • 为什么学习?
    • 1.可以写出自己心中想要的一些小工具
    • 2.工作需要
    • 3.了解MVC架构,了解监听
      在这里插入图片描述

AWT

  • AWT(Abstract Window Toolkit),中文译为抽象窗口工具包该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的图形用户界面的基本工具。
1、Frame
案例一:简单的Frame窗口和属性设置
/**
 * 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(Color.lightGray);
        // 弹出的初始位置
        frame.setLocation(200,200);
        // 设置窗口大小固定
        frame.setResizable(false);
    }
}

在这里插入图片描述

案例二:封装自定义Frame类,并创建多个Frame窗口
/**
 * 展示多个Frame窗口
 **/
public class TestFrame2 {
    public static void main(String[] args) {
        MyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.lightGray);
        MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.BLUE);
        MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.CYAN);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.GREEN);
    }
}

class MyFrame extends Frame{
    /**
     * 可能存在多个窗口,我们需要一个计数器
     **/
    static int id = 0;

    public MyFrame(int x,int y, int w,int h,Color color) throws HeadlessException {
        super("MyFrame:"+(++id));
        setBounds(x,y,w,h);
        setBackground(color);
        setVisible(true);
        setResizable(false);
    }
}

在这里插入图片描述

解决关闭事件
		// 监听事件,监听窗口关闭
        // 适配器模式
        frame.addWindowListener(new WindowAdapter() {
            // 窗口点击关闭时需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                // 结束程序
                System.exit(0);
            }
        });
2、面板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);
        frame.setBackground(Color.BLUE);

        // 设置布局属性
        // 坐标
        panel.setBounds(50,50,100,100);
//        panel.setBounds(50,160,100,100);
        panel.setBackground(Color.RED);

        // frame添加panel组件
        frame.add(panel);
        frame.setVisible(true);

        // 监听事件,监听窗口关闭
        // 适配器模式
        frame.addWindowListener(new WindowAdapter() {
            // 窗口点击关闭时需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                // 结束程序
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

3、布局管理器
3.1、流式布局
/**
 * 流式布局
 **/
public class TestFlowLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();

        // 组件-按钮
        Button btn1 = new Button("Button1");
        Button btn2 = new Button("Button2");
        Button btn3 = new Button("Button3");
        // 设置为流式布局,默认是居中CENTER
//        frame.setLayout(new FlowLayout());
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setSize(300,300);
        frame.setVisible(true);
        // 添加按钮
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        // 窗口监听
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

在这里插入图片描述

3.2、东西南北中
/**
 * 东西南北中
 **/
public class TestBorderLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestBorderLayout");
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

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

在这里插入图片描述

3.3、表格布局
/**
 * 表格布局
 **/
public class TestGridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame("TestGridLayout");
        frame.setSize(300,300);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        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");

        frame.setLayout(new GridLayout(3,2));
        frame.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);
    }
}

在这里插入图片描述

总结
  • 1、Frame是一个顶级窗口
  • 2、Panel无法单独显示,必须添加到某个容器中
  • 3、布局管理器(流式、东西南北中、表格)
  • 4、大小、属性、背景颜色、可见性、关闭监听
4、监听
案例一:按钮监听事件
public class TestActionEvent {
    public static void main(String[] args) {

        Frame frame = new Frame("TestActionEvent");
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));
        frame.setSize(300,300);
        frame.setVisible(true);

        Button button = new Button();
        button.setLabel("aaaaaa");
        button.addActionListener(new MyActionListener());
        windowClose(frame);
        frame.add(button);
        frame.pack();

    }

    /**
     * 关闭窗口方法的封装
     **/
    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("hello");
    }
}
案例二:输入框监听事件
public class jisuanqi {
    public static void main(String[] args) {
        new MyFrame2();
    }
}

class MyFrame2 extends Frame{
    public MyFrame2() throws HeadlessException {
        TextField textField = new TextField();
        add(textField);
        MyAActionListener2 listener2 = new MyAActionListener2();
        // 回车触发
        textField.addActionListener(listener2);
        textField.setEchoChar('*');
        setSize(300,300);
        setVisible(true);
    }
}

class MyAActionListener2 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource(); // 获得资源
        String text = field.getText(); // 获得文本框内的文本内容
        System.out.println(text);
        field.setText("");
    }
}

在这里插入图片描述
在这里插入图片描述

案例三:简易加法计算器
public class TestJiSuanQi {
    public static void main(String[] args) {
        new Calculator();
    }
}

class Calculator extends Frame{
    public Calculator() throws HeadlessException {
        // 三个本文框
        TextField field = new TextField(10);
        TextField field2 = new TextField(10);
        TextField field3 = new TextField(20);
        // 一个按钮
        Button sumBtn = new Button("=");
        MyCalculatorListener myCalculatorListener = new MyCalculatorListener(field,field2,field3);
        sumBtn.addActionListener(myCalculatorListener);
        // 一个标签
        Label label = new Label("+");
        // 布局
        setLayout(new FlowLayout());
        add(field);
        add(label);
        add(field2);
        add(sumBtn);
        add(field3);

        pack();
        setVisible(true);
        windowClose(this);

    }
    // 关闭Frame监听事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}



class MyCalculatorListener implements ActionListener {
    private TextField num1;
    private TextField num2;
    private TextField num3;

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

    @Override
    public void actionPerformed(ActionEvent e) {
        // 获得 加数 和 被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        // 将这两个值加法运算,放到第三个框
        num3.setText(""+(n1+n2));
        // 清除前两个框
        num1.setText("");
        num2.setText("");
    }
}

改造案例三,使用组合的思想(类里面调用另一个类)
public class TestJiSuanQi {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

class Calculator extends Frame{
    // 属性
    TextField num1,num2,num3;
    Button sumBtn;
    Label label;
    // 方法
    public void loadFrame(){
        // 三个本文框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        // 一个按钮
        sumBtn = new Button("=");
        MyCalculatorListener myCalculatorListener = new MyCalculatorListener(this);
        sumBtn.addActionListener(myCalculatorListener);
        // 一个标签
        label = new Label("+");
        // 布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(sumBtn);
        add(num3);

        pack();
        setVisible(true);
        windowClose(this);
    }

    public Calculator() throws HeadlessException {


    }
    // 关闭Frame监听事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}



class MyCalculatorListener implements ActionListener {
    Calculator calculator = null;

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

    @Override
    public void actionPerformed(ActionEvent e) {
        // 获得 加数 和 被加数
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
        // 将这两个值加法运算,放到第三个框
        calculator.num3.setText(""+(n1+n2));
        // 清除前两个框
        calculator.num1.setText("");
        calculator.num2.setText("");
    }
}
改造案例三:使用内部类,好处:内部类可以直接访问外部类的属性
public class TestJiSuanQi {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}

class Calculator extends Frame{
    // 属性
    TextField num1,num2,num3;
    Button sumBtn;
    Label label;

    public Calculator() throws HeadlessException {

    }

    // 方法
    public void loadFrame(){
        // 三个本文框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        // 一个按钮
        sumBtn = new Button("=");
        MyCalculatorListener myCalculatorListener = new MyCalculatorListener();
        sumBtn.addActionListener(myCalculatorListener);
        // 一个标签
        label = new Label("+");
        // 布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(sumBtn);
        add(num3);

        pack();
        setVisible(true);
        windowClose(this);
    }

    // 内部类,好处:可以直接访问外部类
    private class MyCalculatorListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // 获得 加数 和 被加数
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            // 将这两个值加法运算,放到第三个框
            num3.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        } // 清除前两个框

    }

    // 关闭Frame监听事件
    private static void windowClose(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

Swing


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值