第34天学习打卡(GUI编程之组件和容器frame plane 布局管理 事件监听,多个按钮共享一个事件)

GUI编程

组件

窗口

弹窗

面板

文本框

列表框

按钮

图片

监听事件

鼠标

键盘事件

破解工具

1 简介

GUi的核心技术:Swing AWT

1.界面不美观

2.需要jre环境

为什么要学习GUI:

1.可以写出自己心中想要的一些小工具

2.工作时,也可能需要维护到swing界面,概率极小!

3.了解MVC架构,了解监听

2、AWT(抽象的窗口工具)

2.1 Awt介绍

1.包含了很多类和接口!

GUI:图形用户界面

2.元素:窗口,按钮,

3.java.awt

image-20210211075828532

2.2组件和容器

1 Frame
package com.kuang.lesson01;
//GUI的第一个界面

import java.awt.*;

public class TestFrame {
    
    
    public static void main(String[] args) {
    
    
        //Frame JDK
        Frame frame = new Frame("我的第一个Java图形界面窗口");

        //需要设置可见性
        frame.setVisible(true);

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

        //设置背景颜色 Color
        frame.setBackground(new Color(134, 42, 42));

        //弹出的初始位置
        frame.setLocation(200,200);

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



    }
}

image-20210211082732370

问题:发现窗口关闭不掉,停止Java程序

尝试回顾封装

package com.kuang.lesson01;

import java.awt.*;

public class TestFrame2 {
    
    
    public static void main(String[] args) {
    
    
        //展示多个窗口
        MyFrame myFrame1 = new MyFrame(100,100,200,200,Color.blue);
        MyFrame myFrame2 = new MyFrame(300,100,200,200,Color.yellow);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.red);
        MyFrame myFrame4 = 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);

    }


}

image-20210211084342238

2面板Panel

解决了关闭窗口的事件。

package com.kuang.lesson01;

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

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(40, 161, 35));

        //panel 设置坐标,相对于frame(嵌套于frame中)
        panel.setBounds(50, 50, 400, 400);
        panel.setBackground(new Color(193, 15, 60));

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

image-20210211093758519

3 布局管理器

流式布局

package com.kuang.lesson01;

import java.awt.*;

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

       //组件-按钮
        Button button1 = new Button("button1");//当写完一个new对象时按住alt+回车就可以生成左边的等式
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

       // frame.setLayout(new FlowLayout());//按钮的位置是居中
        //frame.setLayout(new FlowLayout(FlowLayout.LEFT));//把按钮的位置设置为居左
        frame.setLayout(new FlowLayout(FlowLayout.RIGHT));//把按钮的位置设置为居右

        frame.setSize(200,200);

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

        frame.setVisible(true);

    }
}

东南西北中

package com.kuang.lesson01;

import java.awt.*;

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

image-20210211102802754

表格布局

package com.kuang.lesson01;

import java.awt.*;

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

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

        frame.pack();//Java函数,相当于自动布局

        frame.setVisible(true);

    }
}

image-20210211103845758

编写的布局

package com.kuang.lesson01;

import java.awt.*;

public class ExDemo {
    
    
    public static void main(String[] args) {
    
    
        //总的Frame
        Frame frame = new Frame();
        frame.setSize(400,300);
        frame.setLocation(300,400);
        frame.setBackground(Color.BLACK);
        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);

        //中间四个
        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);
            }
        });









    }
    }

image-20210211112029468

总结

1.Frame是一个顶级窗口

2.Panel无法单独显示,必须添加到某个容器

3.布局管理器

​ 1.流式

​ 2.东西南北中

​ 3.表格

4.大小,定位,背景颜色,可见性,监听

4 事件监听

事件监听:当某个事情发生的时候,干什么?

package com.kuang.lesson02;

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();
        //因为,addActionListener()需要一个ActionListener,所有我们需要构造一个ActionListener= new MyActionListener();
        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");

    }
}


多个按钮,共享一个事件

package com.kuang.lesson02;

import com.oop.demo05.B;

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

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

        //可以显示的定义触发会返回的命令,如果不显示定义,则会走默认的值
        //可以多个按钮只写一个监听类
        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);
    }
}
class MyMonitor implements ActionListener{
    
    
    @Override
    public void actionPerformed(ActionEvent e){
    
    
        //e.getActionCommand()获得按钮信息
        System.out.println("按钮被点击了: msg=> " + e.getActionCommand());
        if (e.getActionCommand().equals("start")){
    
    

        }


    }
}

猜你喜欢

转载自blog.csdn.net/doudoutj/article/details/113790683
今日推荐