Java-GUI编程(1)AWT

GUI编程

1.AWT

1.1 AWT介绍

Java-GUI编程(1)AWT_第1张图片

1.2 组件和容器

1.Frame

import java.awt.*;

public class FrameTest {
    public static void main(String[] args) {
        Frame frame = new Frame("我的第一个图形界面窗口");
        //设置可见性
        frame.setVisible(true);
        //设置大小
        frame.setSize(400,400);
        //设置背景颜色
        frame.setBackground(new Color(156, 130, 130));

        //默认初始位置
        frame.setLocation(200,200);

        //设置大小固定
        frame.setResizable(false);
    }
}

Java-GUI编程(1)AWT_第2张图片

没有设置关闭方式,停止java程序运行。

封装思想

import java.awt.*;

public class FrameTest2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame frame1=new MyFrame(100,100,200,200, Color.blue);
        MyFrame frame2=new MyFrame(300,100,200,200, Color.yellow);
        MyFrame frame3=new MyFrame(100,300,200,200, Color.red);
        MyFrame frame4=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));
        setBounds(x, y, w, h);
        setVisible(true);
        setBackground(color);
    }
}

Java-GUI编程(1)AWT_第3张图片

2.面板Panel

通过添加事件监听解决窗体无法关闭的问题

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

//Panel 可以看成是一个空间,但不能单独存在
public class PanelTest {
    public static void main(String[] args) {
        Frame frame = new Frame();
        //布局
        Panel panel1 = new Panel();
        Panel panel2 = new Panel();


        //设置布局
        frame.setLayout(null);

        //设置坐标,背景颜色
        frame.setBounds(300, 300, 500, 500);
        frame.setBackground(Color.blue);

        //panel设置坐标,相对于frame
        panel1.setBounds(50, 50, 100, 100);
        panel1.setBackground(new Color(11, 27, 107, 112));

        panel2.setBounds(150, 150, 200, 200);
        panel2.setBackground(new Color(94, 6, 109));

        //frame添加panel  frame.add(panel)
        frame.add(panel1);
        frame.add(panel2);

        frame.setVisible(true);

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

Java-GUI编程(1)AWT_第4张图片

1.3.布局管理器

1.流式布局

FlowLayout

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

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

        //组件---按钮
        Button button1 = new Button("按钮1");
        Button button2 = new Button("按钮2");
        Button button3 = new Button("按钮3");

        //设置为流式布局
        //frame.setLayout(new FlowLayout());   默认为中
        //添加参数
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        frame.setSize(200,200);

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

        frame.addWindowListener(new WindowAdapter() {
            //窗体点击关闭的时候需要做的事情,结束程序
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                //结束程序
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
}

Java-GUI编程(1)AWT_第5张图片

2.边界布局

BorderLayout

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

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

        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");
        //使用东西南北中布局(BorderLayout)添加按钮
        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.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}

Java-GUI编程(1)AWT_第6张图片

3.表格布局

GridLayout

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

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

        Button button1 = new Button("按钮1");
        Button button2 = new Button("按钮2");
        Button button3 = new Button("按钮3");
        Button button4 = new Button("按钮4");
        Button button5 = new Button("按钮5");
        Button button6 = new Button("按钮6");

        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.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        frame.setSize(300,300);
        frame.setVisible(true);
    }
}

Java-GUI编程(1)AWT_第7张图片

4.例题

Java-GUI编程(1)AWT_第8张图片

**分析:**先将窗体分为两个部分,采用表格布局,分为两行一列。再定义四个面板,第一个面板负责第一部分,第一个面板又分为三个部分,采用边界布局,将第一个按钮放在东,第二个按钮放在西,再将定义第二个面板,将其放在正中,把第二个面板定义为表格布局,分为两行一列,再把两个按钮放进去,最后再把第二个面板放到第一个面板中。第二部分同理,最后把第一个面板和第三个面板放到窗体中,完成题目。

代码实现:

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

public class Test1 {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(600,300);
        frame.setLayout(new GridLayout(2,1));
        frame.setVisible(true);

        //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("Button"),BorderLayout.EAST);
        p1.add(new Button("Button"),BorderLayout.WEST);
        p2.add(new Button("Button"));
        p2.add(new Button("Button"));
        p1.add(p2, BorderLayout.CENTER);

        p3.add(new Button("Button"),BorderLayout.EAST);
        p3.add(new Button("Button"),BorderLayout.WEST);
        for (int i = 0; i < 4; i++) {
            p4.add(new Button("Button"));
        }
        p3.add(p4);

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

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}
5.总结
  1. Frame是一个顶级窗体
  2. Panel无法单独显示,必须添加到某个容器中
  3. 三种布局管理器
  4. 大小,定位,可见,监听,背景

1.4.事件监听

事件监听:当某个事件发生,去执行什么操作

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 ActionEventTest {
    public static void main(String[] args) {
        //按下按钮,触发事件
        Frame frame = new Frame();
        Button b1 = new Button();

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

        frame.add(b1,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) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

//事件监听
class MyActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");
    }
}

多个按钮共享一个事件

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 ActionEventTest2 {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        //开始  停止
        Frame frame = new Frame();
        Button b1 = new Button("start");
        Button b2 = new Button("stop");
        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认默认值
        //可以多个按钮只写一个监听类
        b2.setActionCommand("b2.stop");

        MyAction myAction = new MyAction();

        b1.addActionListener(myAction);
        b2.addActionListener(myAction);

        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2,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) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

class MyAction implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()获取按钮的信息
        System.out.println("按钮被点击:"+e.getActionCommand());
    }
}

1.5.输入框事件监听

TextField

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 TextTest01 {
    public static void main(String[] args) {
        //只负责启动
        new MyFrame();
    }
}

class MyFrame extends Frame {
    public MyFrame(){
        TextField textField = new TextField();
        add(textField);

        //监听文本框输入的文字
        MyActionListener2 myActionListener2 = new MyActionListener2();
        //按下enter,就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);

        //设置替换编码
        textField.setEchoChar('*');

        setVisible(true);
        pack();
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }
}

class MyActionListener2 implements ActionListener{
    public void actionPerformed(ActionEvent e) {
        TextField field=(TextField)e.getSource();   //获得一些资源,返回一个对象
        //获得输入框中的文本
        System.out.println(field.getText());
        field.setText("");    //回车清空
    }
}

1.6.简易计算器

组合+内部类

基础实现

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 Calc {
    public static void main(String[] args) {
        new Calculator();
    }
}

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

        //一个按钮
        Button button = new Button("=");
        button.addActionListener(new CalcListener(num1,num2,num3));

        //一个标签
        Label label = new Label("+");

        //流式布局
        setLayout(new FlowLayout());
        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        //setSize(300,100);
        pack();
        setVisible(true);
        //窗体关闭
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }


}

//监听器类
class CalcListener implements ActionListener{
    //获取三个变量
    private TextField num1,num2,num3;

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

    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num1.getText());

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

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

完全改造成面向对象

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 Calc2 {
    public static void main(String[] args) {
        new Calculator2().loadFrame();
    }
}

//计算器类
class Calculator2 extends Frame {
    //属性
    TextField n1,n2,n3;

    //方法
    public void loadFrame(){
        n1 = new TextField(10);  //三个文本框
        n2 = new TextField(10);  //字符数
        n3 = new TextField(20);
        Button button = new Button("=");        //一个按钮
        Label label = new Label("+");           //一个标签

        button.addActionListener(new CalcListener2(this));

        //流式布局
        setLayout(new FlowLayout());
        add(n1);
        add(label);
        add(n2);
        add(button);
        add(n3);

        //setSize(300,100);
        pack();
        setVisible(true);
        //窗体关闭
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }


}

//监听器类
class CalcListener2 implements ActionListener{
    //获取计算器对象
    private Calculator2 calculator2=null;

    public CalcListener2(Calculator2 calculator2){
        this.calculator2=calculator2;
    }

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

使用内部类进行优化

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 Calc3 {
    public static void main(String[] args) {
        new Calculator3().loadFrame();
    }
}

//计算器类
class Calculator3 extends Frame {
    //属性
    TextField n1,n2,n3;

    //方法
    public void loadFrame(){
        n1 = new TextField(10);  //三个文本框
        n2 = new TextField(10);  //字符数
        n3 = new TextField(20);
        Button button = new Button("=");        //一个按钮
        Label label = new Label("+");           //一个标签

        button.addActionListener(new CalcListener3());

        //流式布局
        setLayout(new FlowLayout());
        add(n1);
        add(label);
        add(n2);
        add(button);
        add(n3);

        //setSize(300,100);
        pack();
        setVisible(true);
        //窗体关闭
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }

    //监听器类
    //内部类的最大好处,就是可以畅通无阻的使用外部类的属性和方法
    class CalcListener3 implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数
            //2.将这个值+法运算后,放到第三个框
            // 3.清除前两个框
            int i1 = Integer.parseInt(n1.getText());
            int i2 = Integer.parseInt(n1.getText());
            n3.setText(""+(i1+i2));
            n1.setText("");
            n2.setText("");
        }
    }
}

1.7.画笔

可以画一些图形,也能画字符串

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

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

class MyPaint extends Frame {
    public void loadFrame(){
        setBounds(200,200,600,600);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }

    //画笔
    public void paint(Graphics g){
        //super.paint(g);
        //画笔需要有颜色,可以画画
        //g.setColor(Color.RED);
        //g.drawOval(100,100,100,100);
        //g.fillOval(300,300,100,100); //实心圆

        g.setColor(Color.yellow);
        g.fillRect(200,200,100,100);

        //画笔用完,还原到最初的颜色
    }
}

1.8.鼠标监听

目的:想要实现简易鼠标画画

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

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

class MyFrame extends Frame{
    //画画需要画笔,需要监听鼠标当前位置,需要集合来存储这个点
    ArrayList points;

    public MyFrame(String title){
        super(title);
        setBounds(200,200,400,400);
        //存鼠标点击的点
        points=new ArrayList<>();
        //鼠标监听器,针对这个窗口
        this.addMouseListener(new MyMouseListener());

        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
    }

    public void paint(Graphics g){
        //画画需要监听鼠标的事件
        Iterator iterator =points.iterator();   //迭代器
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.blue);
            g.fillOval(point.x,point.y,10,10);
        }
    }

    //添加一个点到界面上
    public void addPaint(Point point){
        points.add(point);
    }

    private class MyMouseListener extends MouseAdapter {
        //鼠标 按下,弹起,按住不放
        @Override
        public void mousePressed(MouseEvent e) {
            //super.mousePressed(e);
            MyFrame myFrame =(MyFrame) e.getSource();
            //这里点击的时候就会在界面产生一个点
            //这个点就是鼠标的点
            myFrame.addPaint(new Point(e.getX(),e.getY()));

            //每次点击鼠标都要重新画一遍
            myFrame.repaint();//刷新
        }
    }
}

Java-GUI编程(1)AWT_第9张图片

1.9.窗口监听

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

public class WindowListenerTest {
    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() {
                    //关闭窗口
                    public void windowClosing(WindowEvent e) {
                        System.out.println("关闭窗口");
                        System.exit(0);
                    }
                    //激活窗口
                    public void windowActivated(WindowEvent e) {
                        System.out.println("激活窗口");
                    }
                }
        );
    }

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

1.10.键盘监听

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

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

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

        //根据按下的键,执行不同的操作
        this.addKeyListener(
                new KeyAdapter() {
                    @Override
                    //键盘按下
                    public void keyPressed(KeyEvent e) {
                        //获得键盘下的键是哪一个,当前的码
                        int keyCode=e.getKeyCode();
                        if(keyCode==KeyEvent.VK_UP){
                            System.out.println("你按下了上键");
                        }
                    }
                }
        );
    }
}

你可能感兴趣的:(Java-GUI编程(1)AWT)