分享

GUI编程

 imnobody2001 2023-01-17 发布于黑龙江

导论

GUI是什么?

  • GUI就是图形用户界面,是基于图形的界面,windows就是一个图形用户界面的操作系统,而DOS是基于命令提示符的操作系统,GUI编程就是编出一个图形用户界面的软件.
  • GUI:图形用户界面

组件:

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

1.简介

GUI核心

  • Swing
  • Awt

不火的原因

  • 界面不美观
  • 需要jre环境

为什么我们要学习?

  • 可以写成心中想要的一些工具
  • 工作时候,也可能需要维护到Swing界面,概率极小
  • 了解MVC架构,了解监听!

2.AWT

2.1 AWT介绍

  • AWT:抽象的窗口工具
    1.包含很多类和接口!
    2.元素:窗口,按钮,文本框
    3.java.awt

2.2组件和容器

1.Frame(窗口)

package com.chen.lesson01;

import java.awt.*;

//GUI的第一个界面
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(85,150,68));

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

        //设置大小固定
        frame.setResizable(false);
    }
}
JAVA 复制 全屏

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

  • 拓展:回顾封装:
package com.chen.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.MAGENTA);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.red);

    }


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

        }

    }

}

2.Panel(面板)

  • 解决了关闭事件
package com.chen.lesson01;

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

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(0xBD9BAD));

        //panel设置坐标,相当于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);
            }
        });

    }
}

3.布局管理器

  • 流式布局
package com.chen.lesson01;

import java.awt.*;

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

        //组件-按钮
        Button button1 = new Button("button01");
        Button button2 = new Button("button02");
        Button button3 = new Button("button03");

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

        frame.setSize(200,200);

        //frame中添加按钮
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.pack();
        frame.setVisible(true);
    }
}
  • 东西南北中
package com.chen.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.SOUTH);
        frame.add(center,BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

    }
}
  • 表格
package com.chen.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);
    }
}

4.事件监听

当某个时期发生时,页面会干什么

  • 按下按钮出发事件
package com.chen.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("button");
        MyActionListener myActionListener = new MyActionListener();
        button.addActionListener(myActionListener);

        frame.add(button,BorderLayout.CENTER);
        windowClose(frame);
        frame.pack();
        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("abc");
    }
}
  • 两个按钮实现同一个监听
package com.chen.lesson02;

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

public class TestAction02 {
    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.setVisible(true);

    }
}

class myMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了:msg"+e.getActionCommand());
        e.getActionCommand();
    }
}

文本框TextField

package com.chen.lesson02;

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

        //监听文本框的文字
        MyActionListener01 myActionListener01 = new MyActionListener01();
        //按下enter 触发输入框的事件
        textField.addActionListener(myActionListener01);

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

        setVisible(true);
        pack();
    }
}

class MyActionListener01 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField field = (TextField) e.getSource();//获取一些资源,返回的一个对象
        System.out.println(field.getText());//获取输入框的文本
        field.setText("");//后台获取完设置为空
    }
}

3.Swing

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多