JavaSE基础二十一:GUI编程下的AWT展示窗口、面板、监听等

一、GUI 简介

1.概述
GUI:图形用户界面,是指采用图形方式显示的计算机操作用户界面

2.component
component是一个类,译为组件类,有很多的子类,它的子类就是一个个具体的图形类
component子类:窗口类(Frame),面板类(Panel),按钮类(Panel),输入框类(TextField)等

在component中有一个容器叫做container,container是一个抽象类,它可以存放其他的具体组件(如按钮),存放的类型也可以是container,即允许多层嵌套的层次结构。Container类在将组件以合适的形式展示在屏幕上时很有用,它有两个子类,Panel和Window,它们不是抽象类
在这里插入图片描述

3.window和panel
Window对应的类为java.awt.Windows,它可独立于其他Container而存在,它有两个子类, Frame和Dialog。Frame是具有标题(title)和可伸缩的角(resize corner)的窗口(Window)。Dialog则没有菜单条,虽然它能移动,但不能伸缩

Panel对应的类为java.awt.Panel,它标识了一个矩形区域,该区域才允许添加其他的组件,但是Panel必须放在Window或其子类中才能显示

简单来说:想要展示一个图形界面,先将组件(如按钮)添加到面板(Panel)中,再将面板添加到窗口(Frame)上

4.GUI缺点
(1)界面不美观
(2)需要jre环境

二、AWT

1.概述
AWT是GUI中的一部分
AWT:译为抽象窗口工具包,该包提供了一套与本地图形界面进行交互的接口,是Java提供的用来建立和设置Java的图形用户界面的基本工具。

即就是:java.awt中提供了我们上述的GUI设计工具
组件——Component
容器——Container
布局管理器——LayoutManager

在AWT中,所有能在屏幕上显示的组件(component)对应的类,均是抽象类Component的子类或子孙类。这些类均可继承Component类的变量和方法

2.代码

2.1窗口

public class Demo1_Frame {
    public static void main(String[] args) {
        //Frame:窗口
        /*Frame()
          构造一个最初不可见的 Frame 新实例()。
        Frame(String title)
        构造一个新的、最初不可见的、具有指定标题的 Frame 对象。*/
        Frame frame = new Frame("我的第一个图形界面窗口");

        //设置窗口的大小
        frame.setSize(400,400);

        //设置窗口弹出的位置
        frame.setLocation(200,200);

        //设置背景的颜色
        frame.setBackground(new Color(178, 76, 8));

        //设置不可被拉伸
        frame.setResizable(false);

        //设置可见性 这个窗口才能被看见
        frame.setVisible(true);
    }
}

在这里插入图片描述
2.2面板

public class Demo3_Panel {
    public static void main(String[] args) {
//      Panel:面板 是最简单的容器类。应用程序可以将其他组件放在面板提供的空间内,这些组件包括其他面板。
        /*Panel()
        使用默认的布局管理器创建新面板。
        Panel(LayoutManager layout)
        创建具有指定布局管理器的新面板。*/

        Frame frame = new Frame();
        Panel panel = new Panel();

        //设置布局frame.setLayout(LayoutManager msg);
        // LayoutManager是布局的接口
        frame.setLayout(null);

        frame.setBounds(200,200,300,300);
        frame.setBackground(Color.orange);
        frame.setVisible(true);

        //panel设置的位置是相对于窗口的位置
        //Panel继承了Component,所以可以使用Component中的方法,同理,Frame也继承了Component类
        panel.setBounds(50,50,100,200);
        panel.setBackground(Color.blue);
        panel.setVisible(true);

        //将panel添加到窗口中
        frame.add(panel);
    }
}

在这里插入图片描述
2.3流式布局

public class Demo4_FlowLayout {
    public static void main(String[] args) {
        //先介绍按钮 Button 此类创建一个标签按钮。当按下该按钮时,应用程序能执行某项动作

        /*构造方法摘要
        Button()
        构造一个标签字符串为空的按钮。
        Button(String label)
        构造一个带指定标签的按钮。*/

        Frame frame = new Frame();
        frame.setBounds(200, 200, 200, 100);
        frame.setBackground(Color.orange);
        //设置布局
        //FlowLayout流布局用于安排有向流中的组件,这非常类似于段落中的文本行
        /*static int CENTER
          此值指示每一行组件都应该是居中的。
          static int LEFT
          此值指示每一行组件都应该是左对齐的。
          static int RIGHT
          此值指示每一行组件都应该是右对齐的。
        */
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        frame.setVisible(true);

        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
    }
}

在这里插入图片描述
2.4东西南北中布局

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

        Button north = new Button("north");
        Button south = new Button("south");
        Button west = new Button("west");
        Button east = new Button("east");
        Button center = new Button("center");

        frame.setSize(400,400);
        frame.setVisible(true);

        /*static String NORTH
        北区域的布局约束(容器顶部)。
        static String SOUTH
        南区域的布局约束(容器底部)。
        static String WEST
        西区域的布局约束(容器左边)。
        static String EAST
        东区域的布局约束(容器右边)。
        static String CENTER
        中间区域的布局约束(容器中央)。 */
        frame.add(north,BorderLayout.NORTH);
        frame.add(south,BorderLayout.SOUTH);
        frame.add(west,BorderLayout.WEST);
        frame.add(east,BorderLayout.EAST);
        frame.add(center,BorderLayout.CENTER);
    }
}

在这里插入图片描述
2.5列表式布局

public class Demo6_GridLayout {
    public static void main(String[] args) {
        Frame frame = new Frame();
        frame.setSize(800,500);
        frame.setBackground(Color.orange);
        frame.setVisible(true);

        frame.setLayout(new GridLayout(3,2));
        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.add(btn1);
        frame.add(btn2);
        frame.add(btn3);
        frame.add(btn4);
        frame.add(btn5);
        frame.add(btn6);

        frame.pack();//自动排版
    }
}

在这里插入图片描述

2.6事件监听
我们发现,当我们想要关闭程序的时候,点击右上角的关闭是没有作用的,这是因为我们没有给那个按钮赋予意义。系统监测到我们点击按钮了,但是却没有传入命令,系统就什么都没有做

下面来解决这个问题:
当我们点击关闭的时候,系统监听这个按钮,我们赋予它结束虚拟机的功能,当我们点击关闭的时候,系统就会停止虚拟机,这样程序就结束了

public class TestActionEvent {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button();
        //因为,addActionListener()需要一个 ActionListener,所以我们需要构造一个 ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        frame.pack();

        windowClose(frame); //关闭窗口
        frame.setVisible(true);
        
    }

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

}

2.7输入框监听

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();
        //按下enter 就会触发这个输入框的事件
        textField.addActionListener(myActionListener2);

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

        setVisible(true);
        pack();

    }
}

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

在这里插入图片描述
2.8画笔

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

class MyPaint extends Frame{

    public void loadFrame(){
        setBounds(200,200,600,500);
        setVisible(true);
    }

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

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

        //养成习惯,画笔用完,将他还原到最初的颜色
    }
}

在这里插入图片描述
2.9鼠标监听

public class suiyi {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

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

    public MyFrame(String title) {
        super(title);
        setBounds(200,200,400,300);
        //存鼠标点击的点
        points = new ArrayList<>();


        setVisible(true);
        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());

    }

    @Override
    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) {
            MyFrame frame = (MyFrame) e.getSource();
            //这个我们点击的时候,就会在界面上产生一个点!画
            //这个点就是鼠标的点;
            frame.addPaint(new Point(e.getX(),e.getY()));

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

}

在这里插入图片描述
2.10窗口监听

public class suiyi {
    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 windowClosing(WindowEvent e) {
                        System.out.println("windowClosing");
                        System.exit(0);
                    }

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

}

在这里插入图片描述
2.11键盘监听

public class suiyi {
    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(); //不需需要去记录这个数值,直接使用静态属性 VK_XXX
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP) {
                    System.out.println("你按下了上键");
                }
                //根据按下不同操作,产生不同结果;
            }
        });
    }
}

根据按下的键返回相应的值
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值