GUI编程

简介

GUI的核心技术: Swing AWT

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

为什么要学习?

  1. 可以写出自己心中想要的一些小工具
  2. 工作的时候,也可能需要维护到Swing界面,概率极小!
  3. 了解MVC框架,了解监听!

AWT

AWT介绍

  1. 包含了很多类的接口! GUI!
  2. 元素:窗口、按钮、文本框
  3. Java.awt

组件和容器

frame

package com.fei.lesson01;

import com.sun.prism.shader.FillCircle_RadialGradient_PAD_AlphaTest_Loader;

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(87, 224, 38));

        //弹出的初始位置
        frame.setLocation(200 , 200);
        
        //设置大小固定
        frame.setResizable(false);
    }
}


问题:发现窗口关闭不掉,需要停止运行程序

尝试回顾封装

package com.fei.lesson01;

import java.awt.*;

public class TestFrame02 {
    public static void main(String[] args) {
        //展示多个窗口 new

        MyFrame myFrame = new MyFrame(100, 100, 200, 200, Color.BLUE);
        MyFrame myFrame1 = new MyFrame(300, 100, 200, 200, Color.yellow);
        MyFrame myFrame2 = new MyFrame(100, 300, 200, 200, Color.red);
        MyFrame myFrame3 = new MyFrame(300, 300, 200, 200, Color.magenta);

    }

}

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

面板panel

解决了关闭事件

package com.fei.lesson02;

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);
        frame.setBackground(new Color(0x8FE36B));

        // Panel 设置坐标,相对于frame
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(230,1, 253));

        //frame.add(panel)
        frame.add(panel);

        frame.setVisible(true);

        //监听事件,监听窗口关闭事件   System.exit(0)
        frame.addWindowListener(new WindowAdapter() {

            //窗口点击关闭之后需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

布局管理器

  • 流式布局
package com.fei.lesson03;

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

        //设置为流式布局
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        frame.setSize(200, 200);

        //把按钮添加上去
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);

        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

  • 东西南北中
package com.fei.lesson03;

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 north = new Button("North");
        Button south = new Button("South");
        Button center = new Button("Center");

        frame.add(east, BorderLayout.EAST);
        frame.add(west, BorderLayout.WEST);
        frame.add(north, BorderLayout.NORTH);
        frame.add(south, BorderLayout.SOUTH);
        frame.add(center, BorderLayout.CENTER);

        frame.setSize(300 , 300);

        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

  • 表格布局
package com.fei.lesson03;

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 tian = new Button("tian");
        Button nan = new Button("nan");
        Button di = new Button("di");
        Button bei = new Button("bei");
        Button zhong = new Button("zhong");

        frame.setLayout(new GridLayout(3 ,2));

        frame.add(tian);
        frame.add(nan);
        frame.add(di);
        frame.add(bei);
        frame.add(zhong);

        frame.pack(); //java函数
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

课堂练习

package com.fei.lesson03;

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

public class TestDemo {
    public static void main(String[] args) {

        Frame frame = new Frame("TestDemo");

        frame.setSize(500 , 600);
        frame.setLocation(400, 400);
        frame.setBackground(Color.CYAN);

        frame.setVisible(true);

        frame.setLayout(new GridLayout(2,1));

        //4个面板
        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);
        p2.add(new Button("p2-btn-1"));
        p2.add(new Button("p2-btn-2"));
        p1.add(p2, BorderLayout.CENTER);

        //下面
        p3.add(new Button("East-2"), BorderLayout.EAST);
        p3.add(new Button("West-2"), BorderLayout.WEST);
        
        //中间的4个
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("for-" + i));
        }

        p3.add(p4, BorderLayout.CENTER);

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

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}


总结

1. Frame是一个顶级窗口
2. Panel无法单独显示,必须添加到某个容器中
3. 布局管理器
 	1. 流式
  	2. 东西南北中
  	3. 表格
4. 大小、定位、背景颜色、可见性、监听!

事件监听

事件监听:当某个事情发生 的时候,鼠标、键盘------ 干什么?

package com.fei.lesson04;

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 TestAction {
    public static void main(String[] args) {

        //按下按钮, 触发一些事件
        Frame frame = new Frame();
        Button button = new Button();
        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        windowClose(frame);
        frame.setVisible(true);

        //因为addActionListener() 需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);
    }

    //关闭窗体的事件
    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("wangpengfei");
    }
} 

输入框TextField 监听

package com.fei.lesson05;

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

        //监听这个文本框输入的文字
        MyActionListener myActionListener = new MyActionListener();

        //按下 enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener);

        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}

class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource(); //获得一些资源,返回了一个对象
        System.out.println(field.getText());       //获得输入框的文本
        field.setText("");
    }
}

简易计算器、组合 + 内部类回顾复习

原始代码

package com.fei.lesson05;

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

//简易计算器
public class DemoText {
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
    }
}

//计算器类
class Calculator extends Frame{
    public Calculator(){
        //3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new MyCalculatorListener(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 MyCalculatorListener implements ActionListener{

    //获取三个变量

    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) {

        //获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        //将这个值 + 法运算后,放到第三个框
        num3.setText("" + (n1 + n2));

        //清徐前两个框
        num1.setText("");
        num2.setText("");
    }
}

完全面向对象

package com.fei.lesson05;

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 DemoText {
    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(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalculatorListener(this));

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

    }

}

    //监听器类
    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("");
        }
    }
    

内部类

  • 更好的包装
package com.fei.lesson05;

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 DemoText {
    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(20);
        Button button = new Button("=");
        Label label = new Label("+");
        button.addActionListener(new MyCalculatorListener());
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
    }
    //监听器类
    //内部类最大的好处,就是可以畅通无阻的访问外部类的属性和方法
    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("");
        }
    }
}

画笔paint

package com.fei.lesson06;

import java.awt.*;

public class TestPaint {
    public static void main(String[] args) {
        new MyPaint().losdFrame();
    }
}

class MyPaint extends Frame{
    public void losdFrame(){
        setBounds(200,200,600,500);
        setVisible(true);
    }

    //画笔
    @Override
    public void paint(Graphics g) {
//        super.paint(g);
        //画笔需要有颜色,可以画画
        g.setColor(Color.red);
        g.drawOval(100, 100, 100, 100);
        g.fillOval(100, 100, 100,100);  //画一个实心的圆

        g.setColor(Color.GREEN);
        g.fillRect(200, 200, 150, 150);

        //养成习惯,画笔用完将它还原为最初的颜色,默认的是黑色
    }
}

鼠标监听

目的:想要实现鼠标画画

窗口监听

package com.fei.lesson06;

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

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 windowOpened(WindowEvent e) {
                        System.out.println("窗口打开");
                    }
                    //关闭窗口
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("窗口关闭");
                        System.exit(0);
                    }

                    @Override
                    public void windowClosed(WindowEvent e) {
                        super.windowClosed(e);
                    }

                    @Override
                    public void windowIconified(WindowEvent e) {
                        System.out.println("窗口Activated");
                    }

                    @Override
                    public void windowDeiconified(WindowEvent e) {
                        super.windowDeiconified(e);
                    }
                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        super.windowActivated(e);
                    }

                    @Override
                    public void windowDeactivated(WindowEvent e) {
                        super.windowDeactivated(e);
                    }

                    @Override
                    public void windowStateChanged(WindowEvent e) {
                        super.windowStateChanged(e);
                    }

                    @Override
                    public void windowGainedFocus(WindowEvent e) {
                        super.windowGainedFocus(e);
                    }

                    @Override
                    public void windowLostFocus(WindowEvent e) {
                        super.windowLostFocus(e);
                    }

                }
        );
    }

//    class MyWindowListener extends WindowAdapter {
//        @Override
//        public void windowClosing(WindowEvent e) {
//            setVisible(false);//隐藏窗口,通过按钮隐藏窗口
//            System.exit(0); //正常退出
//        }
//    }
}

键盘监听

package com.fei.lesson06;

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(1, 2, 300, 400);
        setVisible(true);

        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的是哪一个键

                int keyCode = e.getKeyCode();
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按下的是上键");
                }

                //根据按下的不同操作,产生的不同的结果
            }
        });
    }
}

Swing

窗口

package com.fei.lesson07;

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

//主窗口
public class DialogDemo extends JFrame {
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(700, 500);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame放东西,容器
        Container container = this.getContentPane();
        //绝对布局
        container.setLayout(null);

        //按钮
        JButton button = new JButton("点击弹出一个对话框");  //创建
        button.setBounds(30 ,30 ,200 ,50);


        //点击这个按钮的时候弹出一个弹窗
        button.addActionListener(new AbstractAction() {   //监听器
            @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.setVisible(true);
        this.setBounds(100 ,100 ,500, 500);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  //设置关闭默认的操作
        Container contentPane = this.getContentPane();
        contentPane.setLayout(null);

        contentPane.add(new Label("飞哥努力学习Java"));
    }
}

MVC模式指的是MVC框架,M指的是业务模型v是指用户界面,C是指控制器,使用MVC的目的是将M和V实现代码分离,从而使同一个程序可以使用不同的表现形式。其中View的定义比较清晰,就是用户界面

posted @ 2022-03-19 13:39  Root-RockMan  阅读(26)  评论(0编辑  收藏  举报