【JavaSE】之GUI编程(上)

简介: 【JavaSE】之GUI编程

前言


本文为Java基础GUI编程相关知识,Java全栈学习路线可参考:【Java全栈学习路线】最全的Java学习路线及知识清单,Java自学方向指引,内含最全Java全栈学习技术清单~


一、GUI简介


GUI的核心技术: Swing, AWT


GUI缺点与不足:


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


为什么要学习GUI?


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


二、AWT


1. AWT介绍


  • 包含了很多类和接口!GUI:图像用户界面。 Eeclipse:java环境写的
  • 元素:窗口、按钮、文本框
  • java.awt包里


2.组件和容器


Frame

package com.wang.lesson1;
import java.awt.*;
//GUI的第一个界面
public class TestFrame {
    public static void main(String[] args) {
        //Frame,JDK  看源码;
        Frame frame = new Frame("我的第一个java图形界面窗口");
        //设置可见性 w h 没有
        frame.setVisible(true);
        //设置窗口大小
        frame.setSize(400, 400);
        //背景颜色 Color
        Color color = new Color(155, 89, 104);
        frame.setBackground(color);
        //弹出的初始位置
        frame.setLocation(200, 200);
        //设置大小固定
        frame.setResizable(false);
    }
}

问题:窗口无法关闭—>需通过强制java停止运行来关闭


面板panel


解决了无法关闭问题,即调用addWindowsListener方法的子方法,并重写其中的WindowsClosing方法,来调用程序关闭的.exit(0)方法

package com.wang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
//Panel 可以看成一个空间,但是不能单独存在,得放在Frame上
public class TsetPanel {
    public static void main(String[] args) {
       //窗口
        Frame frame = new Frame();
        //布局的概念
        //面板
        Panel panel = new Panel();
        /*设置布局  不设置会默认置顶
        未设置Layout时,java默认为flowLayout布局的,
         设置为null即为清空布局管理器,之后添加组件,常常是设置组件左上角坐标相
         对于容器左上角(0,0)的x,y值来确定组件的位置,即使更改容器大小也不会
         改变位置。这种方式常常用于窗体大小固定的容器里。*/
       frame.setLayout(null);
        //坐标
        frame.setBounds(300,300,500,500);
        frame.setBackground(new Color(59, 164, 125));
        //panel 设置坐标,相对于Frame的坐标
        panel.setBounds(50,50,200,200);
        panel.setBackground(new Color(90, 46, 30));
        //frame.add(panel)frame添加面板
        frame.add(panel);//Panel经过三层继承,最终继承了Component
        frame.setVisible(true);
        //监听时间,监听窗口关闭事件   System.exit(0)
        //适配器模式: new 重写的太多了  new其子类 本来new WindowsListener的,但是要重写的实在太多了
        frame.addWindowListener(new WindowAdapter() {
           //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

3.布局管理器


流式布局 从左到右

package com.wang.lesson1;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TsetFlowLayout {
    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.setVisible(true);
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.addWindowListener(new WindowAdapter() {
            //窗口点击关闭的时候需要做的事情
            @Override
            public void windowClosing(WindowEvent e) {
                //结束程序
                System.exit(0);
            }
        });
    }
}

东西南北中

package com.wang.lesson1;
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(300,300);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
    }
}

表格布局三行两列这种Grid

package com.wang.lesson1;
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("TsetGridLayout");
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");
        Button button4 = new Button("button4");
        Button button5 = new Button("button5");
        Button button6 = new Button("button6");
        frame.setLayout(new GridLayout(3,2));
        frame.add(button1);
        frame.add(button2);
        frame.add(button3);
        frame.add(button4);
        frame.add(button5);
        frame.add(button6);
        frame.pack();//让布局变得好看
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

4.事件监听


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

package com.wang.lesson2;
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 button1 = new Button("button1");
        //因为addActionListener需要ActionListener,因此我们需要构造一个ActionListener
        MyActionListener myActionListener = new MyActionListener();
        button1.addActionListener(new MyActionListener());
        frame.add(button1, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        windowsClosing(frame);
    }
    //关闭窗体的事件
    private static void windowsClosing(Frame frame){
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
//事件监听
    static class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("11");
        }
    }
}

多个按钮共享一个事件

package com.wang.lesson2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
public class TestActionEvent2 {
    public static void main(String[] args) {
        //两个按钮,实现同一个监听
        Frame frame = new Frame("1111");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        //可以显示的定义出发会返回的命令,如果不显示定义,则会走默认值
        //可以多个按钮只写一个监听类
        button1.setActionCommand("3333");
        My my = new My();
        button1.addActionListener(my);
        button2.addActionListener(my);
        frame.add(button1, BorderLayout.WEST);
        frame.add(button2, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
    }
    static class My implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            //e.getActionCommand()获得按钮的信息
            System.out.println("按钮被点击了:msg" + e.getActionCommand());
        }
    }
}


5.输入框事件监听


  • 输入框中输入的字,可以打印出来,并将输入的字全部删除。


package com.wang.lesson2;
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);
        //监听这个文本框输入的文字
        //按下回车键,就会触发这个输入框的事件,在下边的重写方法中重写的语句为  获得输入框的文本并打印
        textField.addActionListener(new My());
        //设置替换编码
        textField.setEchoChar('*');
        setVisible(true);
        pack();
    }
}
class My implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        TextField text = (TextField) e.getSource();//获得一些资源,返回的一个对象
        System.out.println(text.getText());//获得输入框的文本
        //每次都设置为空 即每次文本框输入完以后,都会全部删除清零
        text.setText("");
    }
}

6.画笔

package com.wang.lesson3;
import java.awt.*;
public class TestPaint {
    public static void main(String[] args) {
        new MYpaint().loadFrame1();
    }
}
class MYpaint extends Frame{
    public void loadFrame1(){
        setBounds(200,200,600,400);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        //super.paint(g);有些类的父类有一些初始化操作,不能随便干掉
        //画笔,需要颜色,画笔可以画画
        g.setColor(Color.red);
        g.drawOval(100,100,100,100);
        g.fillOval(200,200,100,100);//实心的⚪
        g.setColor(Color.green);
        g.fillRect(300,300,40,20);
        g.drawRect(300,350,40,20);
        //养成习惯 画笔画完,将他还原到最初的颜色
        g.setColor(Color.BLACK);
    }
}


7.鼠标监听


以实现鼠标画画为例:

package com.wang.lesson3;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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(100, 100, 500, 400);
        //存鼠标的点
        points = new ArrayList<>();
        //鼠标监听器,针对这个窗口
        setVisible(true);
        this.addMouseListener(new MyML());
    }
    @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 MyML extends MouseAdapter {
        //鼠标 按下,弹起,按住不放
        @Override
        public void mouseClicked(MouseEvent e) {
            MyFrame myframe = (MyFrame) e.getSource();
            //这里我们点击的时候,就会在界面产生一个点
            myframe.addPaint(new Point(e.getX(),e.getY()));
            //每次点击鼠标都需要重新画一遍
            myframe.repaint();//刷新
        }
    }
}

8.窗口监听

package com.wang.lesson3;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
    public static void main(String[] args) {
        new WindowF();
    }
}
class WindowF extends Frame {
    public WindowF() {
        setBackground(Color.BLUE);
        setBounds(100, 100, 200, 200);
        setVisible(true);
        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("windowsClosing");
                        System.exit(0);
                    }
                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowF source = (WindowF) e.getSource();
                        source.setTitle("已激活");
                        System.out.println("windowActivated");
                    }
                });
    }
           /* @Override
            public void windowClosing(WindowEvent e) {
                setVisible(false);// 隐藏窗口
                System.exit(0);//正常退出   1是非正常退出
            };*/
}


9.键盘监听


package com.wang.lesson3;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLOutput;
//键
public class TestKeyListener {
    public static void main(String[] args) {
        new KeyF();
    }
}
class KeyF extends Frame{
    public KeyF(){
        setBounds(0,0,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("你按了上键盘");
                   //根据不同的操作,进行不同的结果
               }
           }
        });
    }
}


相关文章
|
3月前
|
前端开发 Java 数据安全/隐私保护
Java学习—GUI编程学习笔记
Java学习—GUI编程学习笔记
|
7月前
|
容器
|
7月前
|
7月前
|
数据安全/隐私保护
|
1月前
|
前端开发 Java 开发工具
盘点Java GUI框架
盘点Java GUI框架
39 0
|
7月前
|
3月前
|
前端开发 Java 程序员
【JAVA】<GUI编程>AWT & Swing 图形化编程库
【1月更文挑战第19天】【JAVA】<GUI编程>AWT & Swing 图形化编程库
|
7月前
|
8月前
|
IDE Java 程序员
为什么都2022年了还有人用Java写GUI?
为什么都2022年了还有人用Java写GUI?
161 0
|
10月前
|
Java 容器
【Java_GUI编程】--基本操作你要知道(一)
【Java_GUI编程】--基本操作你要知道(一)
87 0

相关实验场景

更多