9. GUI编程

GUI编程

1、 简介

【相关组件】:

  1. 窗口
  2. 弹窗
  3. 面板
    4.文本框
    5.列表框
    6.按钮
    7.图片
    8.监听事件
    9.鼠标
    10.键盘事件
    11.破解工具
  • GUI核心开发技术:Swing和AWT,

  • 不流行原因:1. 界面不美观;2. 需要jre环境!

  • 为什么要学习?

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

2、 AWT

2.1、Awt介绍

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

  2. GUI:图形用户界面编程;

  3. 元素: 窗口,按钮,文本框java.awt包里

2.2、组件和容器

1、Frame

package GUI;

import java.awt.*;

//GUI的第一个界面
public class TestFrame1 {
    public static void main(String[] args) {
        //Frame,JDK,    看源码!
        Frame frame = new Frame("我的第一个Java图像界面窗口");

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

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

        //设置背景颜色
//        frame.setBackground(Color.blue);  //第一种设置背景颜色,使用默认的背景颜色
        frame.setBackground(new Color(45, 52, 57));

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

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

结果显示:

  • 创建多个界面
package GUI;

import java.awt.*;

//生成多个GUI界面
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.green);
        MyFrame myFrame3 = new MyFrame(100,300,200,200,Color.yellow);
        MyFrame myFrame4 = new MyFrame(300,300,200,200,Color.red);
    }
}

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

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

public class TestPanel1 {
    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
        panel.setBounds(50,50,400,400);
        panel.setBackground(new Color(194,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 GUI;

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

public class TestFrameLayout1 {
    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(400,400);

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

        frame.setVisible(true);

        //停止启动
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

输出结果显示:

  • 东西南北中
package GUI;

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

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(400,400);

        frame.setVisible(true);


        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

输出结果显示:

  • 表格布局
package GUI;

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

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

        Button bth1 = new Button("bth1");
        Button bth2 = new Button("bth2");
        Button bth3 = new Button("bth3");
        Button bth4 = new Button("bth4");
        Button bth5 = new Button("bth5");
        Button bth6 = new Button("bth6");

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

        frame.add(bth1);
        frame.add(bth2);
        frame.add(bth3);
        frame.add(bth4);
        frame.add(bth5);
        frame.add(bth6);

        frame.pack();   //Java函数,自动填充
        frame.setVisible(true);

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

输出结果显示:

实例1:

package GUI;

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

public class EXDome {
    public static void main(String[] args) {
        //总 Frame
        Frame frame = new Frame();
        frame.setSize(400,400);
        frame.setLocation(300,400);
//        frame.setBackground(Color.blue);
        frame.setBackground(new Color(65, 209, 221));
        frame.setVisible(true);
        frame.setLayout(new GridLayout(2,1));

        //4个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p5 = new Panel(new GridLayout(1,1));
        Panel p3 = new Panel(new BorderLayout());
        Panel p4 = new Panel(new GridLayout(2,2));
        Panel p2 = new Panel(new GridLayout(1,2));
        Panel p6 = new Panel(new GridLayout(2,1));

        //上面ok
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p5-bth-1"));
        p2.add(new Button("p5-bth-2"));
        p5.add(new Button("p2-bth-1"));
//        p2.add(new Button("p2-bth-2"));
        p6.add(p2,BorderLayout.CENTER);
        p6.add(p5,BorderLayout.CENTER);
        p1.add(p6,BorderLayout.CENTER);

        //下面
        p3.add(new Button("East-1"),BorderLayout.EAST);
        p3.add(new Button("West-1"),BorderLayout.WEST);
        //中间4个
        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);
            }
        });
    }
}

输出结果显示:

实例2:

package GUI;

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

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

        //上面ok
        p1.add(new Button("East-1"),BorderLayout.EAST);
        p1.add(new Button("West-1"),BorderLayout.WEST);
        p2.add(new Button("p2-bth-1"));
        p2.add(new Button("p2-bth-2"));
        p1.add(p2,BorderLayout.CENTER);

        //下面
        p3.add(new Button("East-1"),BorderLayout.EAST);
        p3.add(new Button("West-1"),BorderLayout.WEST);
        //中间4个
        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);
            }
        });
    }
}

输出结果显示:

总结:

  1. Frame是一个顶级窗口;
  2. Panel无法单独显示,必须添加到某个容器;
  3. 布局管理器
    • 流式
    • 东西南北中
    • 表格
  4. 大小,定位,背景颜色,可见性,监听!

2.4、事件监听

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

实现事件监听
package GUI;

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 TestActionEvent1 {
    public static void main(String[] args) {
        //按下按钮,触发一些事件
        Frame frame = new Frame();
        Button button = new Button("button1");
        //因为,addactionlistener()需要一个ActionListener,所以我们需要构造一个ActionListener
        MyActingLinstener myActingLinstener = new MyActingLinstener();
        button.addActionListener(myActingLinstener);

        frame.add(button);
        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 MyActingLinstener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("aaa");  //创建监听事件
    }
}

输出结果显示:

两个按钮同时实现一个监听
package GUI;

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 TestActionTwo {
    public static void main(String[] args) {
        Frame frame = new Frame("启动-停止");  //创建一个顶级窗口
        frame.setLayout(new GridLayout(2,1));  //将顶级窗口弄成表格布局
        frame.setSize(400,400);  //设置窗口大小
        frame.setLocation(200,200);  //设置窗口出现位置
        frame.setVisible(true);   //设置顶级窗口可见

        //创建两个面板
        Panel p1 = new Panel(new BorderLayout());
        Panel p2 = new Panel(new BorderLayout());

        //创建两个按钮
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        button2.setActionCommand("button2-stop");  //设置需要接收的字符串
        MyMonitor myMonitor = new MyMonitor();  //new一个事件

        //将两个按钮都指向一个事件
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        //将两个按钮分别加入到p1,p2面板中,并以表格布局方式布局
        p1.add(button1 ,BorderLayout.CENTER);
        p2.add(button2,BorderLayout.CENTER);

        //将两个面板加入到顶级窗口中
        frame.add(p1);
        frame.add(p2);
        WindosExit(frame); //调用关闭窗口事件

    }
    //创建一个窗口关闭事件
    private static void WindosExit(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

//创建一个事件
class MyMonitor implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("按钮被点击了:msg=>"+e.getActionCommand());
    }
}

输出结果显示:

2.5、输入框TextField 监听

package GUI;

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 TsetText1 {
    public static void main(String[] args) {
        //启动!
        new MyFrame1();
    }
}

class MyFrame1 extends Frame{
    public MyFrame1(){
        TextField textField = new TextField();  //创建文本框
        add(textField); //将文本框加入到顶级窗口里

        MyActionListener2 myActionListener2 = new MyActionListener2(); //监听这个文本框输入的文字
        textField.addActionListener(myActionListener2);//按下enter  就会出发这个输入框的事件
        textField.setEchoChar('*');//设置替换编码
        setVisible(true);  //显示窗口文件
        pack();  //顶级窗口适应大小

        //创建关闭窗口事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {

        //(TextField)e.getSource():强转
        TextField field = (TextField)e.getSource();// 获得一些资源,返回的一个对象
        System.out.println(field.getText()); //获得输入框的文本
        field.setText("");  //PS:null:表示对象;  "":表示为空
    }
}

输出结果显示:

2.6、简易计算器,组合+内部类回顾复习!

oop原则:组合,大于继承!

class A extends B{
    
}

class A{
    public B b;
}
  • 简易计算器(原始代码)
package GUI;

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

//计算器类
class calculator extends Frame{
    public calculator() {
        //3个文本框
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

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

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

        //布局,流式布局
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);
        pack();
        setVisible(true);
		
        //关闭窗口事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

//监听器类
class CalsActionListener implements ActionListener{

    //获取三个变量
    private TextField num1,num2,num3;

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

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

        //2. 将这个值 + 法后运算后,放到第三个框
        num3.setText(""+(n1+n2));
        //3. 清楚前两个框
        num1.setText("");
        num2.setText("");
    }
}

输出结果显示:

  • 简易计算器(优化代码)
package GUI;

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

//计算器类
class Calculator extends Frame{

    //属性
    TextField num1,num2,num3;

    //方法
    public void loadFrame() {
        //3个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("="); //1 个按钮
        Label label = new Label("+"); //1 个标签
        button.addActionListener(new CalsActionListener(this));




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

        //创建关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

//监听器类
class CalsActionListener implements ActionListener{

    //获取计算器这个对象,在一个类中组合另外一个类;
    Calculator calculator= null;

    public CalsActionListener(Calculator calculator){
        this.calculator = calculator;
    }

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

输出结果显示:

  • 简易计算器(面向对象编程)

【内部类】:更好的封装类。

package GUI;

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

}
//计算器类
class Calculator extends Frame{

    //属性
    TextField num1,num2,num3;

    //方法
    public void loadFrame() {
        //3个文本框
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("="); //1 个按钮
        Label label = new Label("+"); //1 个标签
        button.addActionListener(new CalsActionListener());


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

        //创建关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    //监听器类
    private class CalsActionListener implements ActionListener{

        //获取计算器这个对象,在一个类中组合另外一个类;

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

输出结果显示:

2.7、画笔

package GUI.lesson01;

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

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

class MyPaint extends Frame{
    public void loadFrame(){
        setTitle("画笔");   //设置顶级窗口名称
        setBounds(200,200,600,600);  //设置顶级窗口出现位置及大小
        setVisible(true);  //显示顶级窗口可以见

        //窗口关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    //画笔
    @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.8、鼠标监听

package GUI.lesson01;

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 TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}

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

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

        //鼠标监听器,正对这个窗口
        this.addMouseListener(new MyMouseListener());
        
        //关闭事件
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    @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.9、窗口监听

package GUI.lesson01;

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

public class TestWindow {
    public static void main(String[] args) {
        new WindowFram();
    }
}

class WindowFram extends Frame{
    public WindowFram(){
        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");
//                        setVisible(false);  //隐藏窗口,通过按钮,隐藏当前窗口
                        System.exit(0);
                    }

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

输出结果显示:

2.10、键盘监听

package GUI.lesson01;

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

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

class KeyFrame extends Frame{
    public KeyFrame(){
        setTitle("键盘监视器");  //顶级窗口标题
        setBounds(1,2,500,500);  //顶级窗口出现位置及大小
        setBackground(Color.CYAN);  //设置窗口颜色
        setVisible(true);  //显示窗口界面
        //创建关闭窗口监听事件
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //创建键盘监听事件
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个,当前十六进制的码
                int keyCode = e.getKeyCode();  //不需要去记录这个数值,直接使用静态属性 VK_XXXX
                System.out.println(keyCode);
                if (keyCode == KeyEvent.VK_UP){
                    System.out.println("你按下了上键");
                }
                //根据按下后的不同操作,参数不同结果
            }
        });
    }
}

输出结果:

3、Swing

3.1、窗口、面板

package GUI.lesson01;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo {
    //init();初始化
    public void init(){

        JFrame jf = new JFrame("这是一个JFrame窗口"); //顶级窗口
        jf.setVisible(true);
        jf.setBounds(100,100,400,400);
        jf.setBackground(Color.yellow);

        JLabel label = new JLabel("欢迎来到英雄联盟");  //设置文字Jlabel

        jf.add(label);


        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //关闭事件
    }

    public static void main(String[] args) {
        new JFrameDemo().init();
    }
}

输出结果显示:

标签居中

package GUI.lesson01;

import javax.swing.*;
import java.awt.*;

public class JFrameDemo02 {
    public static void main(String[] args) {
        new MyFrame2().init();
    }
}
class MyFrame2 extends JFrame{
    public void init(){
        this.setBounds(100,100,400,400);
        this.setVisible(true);


        JLabel label = new JLabel("欢迎来到英雄联盟");  //设置文字Jlabel

        this.add(label);

        label.setHorizontalAlignment(SwingConstants.CENTER);    //让文本标签剧中   设置水平对齐

        Container contentPane = this.getContentPane();  //获得一个容器
        contentPane.setBackground(Color.CYAN);

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  //创建关闭事件
    }
}

posted @ 2020-12-16 14:10  菜园子_的_码农  阅读(103)  评论(0编辑  收藏  举报