GUI

GUI编程

组件

  • 窗口
  • 弹窗
  • 面板
  • 文本框
  • 列表框
  • 按钮
  • 图片
  • 监听事件
  • 鼠标
  • 键盘事件
  • 外挂
  • 破解工具

简介

GUI的核心技术:Swing、AWT

缺陷:

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

AWT

  • 包含了很多类和接口 GUI:图形用户界面编程
  • 元素:窗口,按钮,文本框

核心类

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fp3fSDg0-1616740109703)(D:\英雄时刻\934947443\FileRecv\MobileFile\Screenshot_2021-03-24-10-57-53-251_com.bilibili.a.png)]

Frame

一个窗口

import java.awt.*;

//GUI的第一个界面
public class TextFrame {
    public static void main(String[] args) {

        //Frame
        Frame frame = new Frame("java图形界面窗口");

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

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

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

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

封装+多个窗口

import java.awt.*;

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0eFHUA9C-1616740109705)(C:\Users\秦敬卓\Desktop\1.png)]

面板Panel

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

//Panel 可以看成是一个空间,但是不能单独存在
public class TextPanel {
    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.red);

        //panel设置坐标,相对于frame
        panel.setBounds(0,0,250,250);
        panel.setBackground(Color.GREEN);

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

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UnRb9XpI-1616740109706)(C:\Users\秦敬卓\Desktop\2.png)]

布局管理器

  • 流式布局
import java.awt.*;

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());默认居中显示
        //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);
    }
}
  • 东西南北中
import java.awt.*;

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

        frame.setSize(400,400);
        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.setVisible(true);
    }
}
  • 表格布局
import java.awt.*;

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

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

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

        frame.add(button1);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);

        frame.pack();//java函数  自动填充
        frame.setVisible(true);
    }
}

测试

import java.awt.*;

public class Work {
    public static void main(String[] args) {
        Frame frame = new Frame("work");
        frame.setSize(600,500);
        frame.setVisible(true);

        Panel panel1 = new Panel();
        Panel panel2 = new Panel();
        Panel panel3 = new Panel();
        Panel panel4 = new Panel();
        Panel panel5 = new Panel();
        panel1.setLayout(new GridLayout(2,1));
        panel2.setLayout(new GridLayout(2,1));
        panel3.setLayout(new GridLayout(2,1));
        panel4.setLayout(new GridLayout(2,1));
        panel5.setLayout(new GridLayout(2,2));

        frame.add(panel1,BorderLayout.WEST);
        frame.add(panel2,BorderLayout.EAST);
        frame.add(panel3,BorderLayout.CENTER);

        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");
        Button btn7 = new Button("btn7");
        Button btn8 = new Button("btn8");
        Button btn9 = new Button("btn9");
        Button btn10 = new Button("btn10");

        panel1.add(btn1);
        panel1.add(btn2);
        panel2.add(btn3);
        panel2.add(btn4);
        panel3.add(panel4);
        panel3.add(panel5);
        panel4.add(btn5);
        panel4.add(btn6);
        panel5.add(btn7);
        panel5.add(btn8);
        panel5.add(btn9);
        panel5.add(btn10);

    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sp6aD801-1616740109708)(C:\Users\秦敬卓\Desktop\3.png)]

事件监听

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

        //因为addActionListener()需要一个ActionListener,所以构造一个ActionListener
        button.addActionListener(new MyActionListener());
        frame.add(button);
        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("a");
    }
}

多个按钮共享一个事件监听

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 TestActionEvent02 {
    public static void main(String[] args) {
        //两个按钮使用一个监听
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        //可以得到按钮定义的信息,如果没定义则得到按钮默认值
        //可以多个按钮只写一个监听类
        button1.setActionCommand("go");

        frame.setLayout(new GridLayout(1,2));
        frame.add(button1);
        frame.add(button2);



        MyActionListener02 myActionListener = new MyActionListener02();

        button1.addActionListener(myActionListener);
        button2.addActionListener(myActionListener);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
}
class MyActionListener02 implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("go")){
            System.out.println("start");
        }
        else {
            System.out.println("stop");
        }
    }
}

输入框 TextField 监听

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

public class TestText {
    public static void main(String[] args) {
        //启动
        MyFrame myFrame = new MyFrame();
    }
}
class MyFrame extends Frame{
    public MyFrame() throws HeadlessException {
        TextField textField = new TextField();
        add(textField);

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

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

        setVisible(true);
        pack();
    }
}
class MyActiongListen03 implements ActionListener{

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

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

        setVisible(true);
        pack();
    }
}
class MyActiongListen03 implements ActionListener{

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值