Java——GUI编程

article/2025/9/23 12:32:16

GUI编程

    • 1、简介
    • 2、AWT(抽象的窗口工具)
      • AWT简介
      • 1、Frame的使用:
      • 2、面板 panel
      • 3、布局管理器
        • 3.1 流式布局
        • 3.2 东西南北中
        • 3.3 表格布局
      • 4、事件监听
      • 5、文本框(TextField)的监听
      • 6、制作简易计算器
      • 7、画笔
      • 8、鼠标监听
      • 9、窗口监听
      • 10、键盘监听
    • 3、Swing
      • 1、窗口,面板
      • 2、弹窗
      • 3、标签
      • 4、面板
      • 5、按钮
      • 6、列表
      • 7、文本框

组件:窗口,弹窗,面板,文本框,列表框,按钮,图片,监听事件,鼠标事件,键盘事件,破解工具。

1、简介

GUI(图形用户界面)的核心技术:Swing,AWT。

不流行:界面不美观,需要jre环境。

学习GUI:可以写一些小工具,维护swing的界面,了解MVC架构,了解监听。

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

AWT简介

1.包含了很多类和接口
2.元素:窗口,按钮,文本框
3.java.awt
在这里插入图片描述

1、Frame的使用:

import java.awt.*;public class Solution {public static void main(String[] args) {//Frame.JDK  看源码Frame frame = new Frame("我的第一个Java图形界面窗口");//需要设置可见性frame.setVisible(true);//设置窗口大小frame.setSize(400, 400);//设置背景颜色frame.setBackground(new Color(104, 42, 42));//弹出初始位置frame.setLocation(200,400);//设置窗口大小固定frame.setResizable(false);}
}

在这里插入图片描述
问题:窗口无法关闭

设置多个窗口:

import java.awt.*;public class Solution {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.pink);MyFrame myFrame4= new MyFrame(300, 300, 200, 200, Color.pink);}
}class MyFrame extends Frame {static int id = 0;public MyFrame(int x, int y, int w, int h, Color color) {super("MyFrame" + (++id));setBounds(x, y, w, h);setBackground(color);setVisible(true);}
}

在这里插入图片描述

2、面板 panel

在容器里边,不能单独存在,需要放在frame上。

import java.awt.*;public class Solution {public static void main(String[] args) {Frame frame = new Frame();//布局的概念Panel panel = new Panel();//设置布局frame.setLayout(null);frame.setBounds(300, 300, 400, 400);//frame.setBackground(new Color(239, 217, 7, 119));//panel设置坐标,相对于Framepanel.setBounds(50, 50, 200, 200);panel.setBackground(new Color(199, 20, 20));frame.add(panel);frame.setVisible(true);}
}

在这里插入图片描述
监听事件关闭:退出

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Solution {public static void main(String[] args) {Frame frame = new Frame();//布局的概念Panel panel = new Panel();//设置布局frame.setLayout(null);frame.setBounds(300, 300, 400, 400);//frame.setBackground(new Color(239, 217, 7, 119));//panel设置坐标,相对于Framepanel.setBounds(50, 50, 200, 200);panel.setBackground(new Color(199, 20, 20));frame.add(panel);frame.setVisible(true);//关闭事件frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}

3、布局管理器

3.1 流式布局

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Solution {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.setBounds(200, 200, 300, 300);//把按钮添加进去frame.add(button1);frame.add(button2);frame.add(button3);frame.setVisible(true);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(1);}});}
}

在这里插入图片描述

3.2 东西南北中

在这里插入图片描述

package demopacket;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Solution {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");Button button4 = new Button("button4");Button button5 = new Button("button5");//设置为东西南北中布局frame.setLayout(new BorderLayout());frame.setBounds(200, 200, 300, 300);//把按钮添加进去frame.add(button1,BorderLayout.EAST);frame.add(button2,BorderLayout.WEST);frame.add(button3,BorderLayout.SOUTH);frame.add(button4,BorderLayout.NORTH);frame.add(button5,BorderLayout.CENTER);frame.setVisible(true);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(1);}});}
}

在这里插入图片描述

3.3 表格布局

package demopacket;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Solution {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");Button button4 = new Button("button4");Button button5 = new Button("button5");//设置为表格布局frame.setLayout(new GridLayout(3, 2));frame.setBounds(200, 200, 300, 300);//把按钮添加进去frame.add(button1);frame.add(button2);frame.add(button3);frame.add(button4);frame.add(button5);//frame.pack();//java函数:作用 自动布局,选择一个最好的frame.setVisible(true);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(1);}});}
}

在这里插入图片描述

4、事件监听

当事情发生的时候,需要干什么?

package demopacket;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 Solution {public static void main(String[] args) {Frame frame = new Frame();Button button = new Button("button");button.addActionListener(new MyAction());frame.setLayout(new BorderLayout());frame.add(button, BorderLayout.CENTER);frame.pack();frame.setVisible(true);frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}
}class MyAction implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("000");}
}

按下会打印000。
在这里插入图片描述

5、文本框(TextField)的监听

package demopacket;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Solution {public static void main(String[] args) {MyFrame myFrame = new MyFrame();}
}class MyFrame extends Frame {public MyFrame() {TextField textField = new TextField();add(textField);//监听这个文本框的文字,按下enter就会触发这个输入框的事件textField.addActionListener(new MyActionListener());//设置替换编码,隐藏输入内容textField.setEchoChar('*');setVisible(true);pack();}
}class MyActionListener implements ActionListener {@Overridepublic void actionPerformed(ActionEvent e) {TextField source =(TextField) e.getSource();  //获得资源,返回一个对象System.out.println(source.getText());source.setText(""); //null,enter后清空文本内容}
}

6、制作简易计算器

package demopacket;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Solution {public static void main(String[] args) {MyFrame myFrame = new MyFrame();}
}class MyFrame extends Frame {public MyFrame() {TextField textField1 = new TextField(10);TextField textField2 = new TextField(10);TextField textField3 = new TextField(20);Button button = new Button("=");Label label = new Label("+");button.addActionListener(new MyActionListener(textField1, textField2, textField3));setLayout(new FlowLayout());add(textField1);add(label);add(textField2);add(button);add(textField3);setVisible(true);pack();}
}class MyActionListener implements ActionListener {private TextField textField1, textField2, textField3;public MyActionListener(TextField textField1, TextField textField2, TextField textField3) {this.textField1 = textField1;this.textField2 = textField2;this.textField3 = textField3;}@Overridepublic void actionPerformed(ActionEvent e) {int a = Integer.parseInt(textField1.getText());int b = Integer.parseInt(textField2.getText());textField3.setText("" + (a + b));textField1.setText("");textField2.setText("");}
}

7、画笔

package demopacket;import java.awt.*;public class Solution {public static void main(String[] args) {MyPrint myPrint = new MyPrint();myPrint.loadFrame();}
}class MyPrint extends Frame {public void loadFrame() {setBounds(200, 200, 300, 300);setVisible(true);}//画笔@Overridepublic void paint(Graphics g) {//画笔需要有颜色,画笔可以画画g.setColor(Color.blue);g.fillOval(100, 100, 100, 100);}
}

8、鼠标监听

package demopacket;import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;public class Solution {public static void main(String[] args) {MyPrint myPrint = new MyPrint("画图");}
}class MyPrint extends Frame {ArrayList points;public MyPrint(String title) {super(title);setBounds(200, 200, 300, 300);points = new ArrayList<>();//鼠标监听器,针对的是这个窗口this.addMouseListener(new MyMouseListener());setVisible(true);}//画笔@Overridepublic 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 {@Overridepublic void mousePressed(MouseEvent e) {MyPrint source = (MyPrint) e.getSource();source.addPaint(new Point(e.getX(), e.getY()));source.repaint();}}}

在这里插入图片描述

9、窗口监听

package demopacket;import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Solution {public static void main(String[] args) {WindowFrame windowFrame = new WindowFrame();}static class WindowFrame extends Frame {public WindowFrame() {setBounds(100, 100, 200, 200);setBackground(Color.blue);setVisible(true);//addWindowListener(new MyWindowListener());this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.out.println("你点击了X");System.exit(0);}});}/*class MyWindowListener extends WindowAdapter {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}}*/}
}

10、键盘监听

package demopacket;import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;public class Solution {public static void main(String[] args) {KeyFrame windowFrame = new KeyFrame();}static class KeyFrame extends Frame {public KeyFrame() {setBounds(100, 100, 200, 200);setBackground(Color.blue);setVisible(true);this.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {System.out.println(e.getKeyCode());}});} }
}

3、Swing

1、窗口,面板

package demopacket;import javax.swing.*;public class Solution {public static void init() {JFrame jFrame = new JFrame("这是一个窗口");jFrame.setVisible(true);jFrame.setBounds(100, 100, 200, 200);JLabel jLabel = new JLabel("你好!!!");jFrame.add(jLabel);jLabel.setHorizontalAlignment(0);//居中jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭事件}public static void main(String[] args) {init();}
}

2、弹窗

package demopacket;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Solution extends JFrame {public Solution() {this.setVisible(true);this.setBounds(100, 100, 200, 200);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭//JFrame 放东西需要一个容器Container container = this.getContentPane();//绝对布局container.setLayout(null);JButton jButton = new JButton("点击弹出一个对话框");jButton.setBounds(30, 30, 100, 50);//点击这个按钮的时候弹出一个弹窗container.add(jButton);jButton.addActionListener(new ActionListener() {//监听器@Overridepublic void actionPerformed(ActionEvent e) {MyDialog myDialog = new MyDialog();}});}public static void main(String[] args) {Solution solution = new Solution();}}class MyDialog extends JDialog {public MyDialog() {this.setVisible(true);this.setBounds(200, 200, 200, 200);Container container = this.getContentPane();//JFrame 放东西需要一个容器container.setLayout(null);JLabel jLabel = new JLabel("你好!!!");jLabel.setBounds(50, 50, 150, 150);container.add(jLabel);}
}

在这里插入图片描述

3、标签

package demopacket;import javax.swing.*;
import java.awt.*;public class Solution extends JFrame implements Icon {private int width;private int height;public Solution() {}public void init() {Solution solution = new Solution(15, 15);//图标可以放在标签上,按钮上JLabel jLabel = new JLabel("hello", solution, SwingConstants.CENTER);Container container = getContentPane();container.add(jLabel);this.setVisible(true);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public Solution(int width, int height) {this.width = width;this.height = height;}public static void main(String[] args) {new Solution().init();}@Overridepublic void paintIcon(Component c, Graphics g, int x, int y) {g.fillOval(x, y, width, height);}@Overridepublic int getIconWidth() {return this.width;}@Overridepublic int getIconHeight() {return this.height;}
}

4、面板

package demopacket;import javax.swing.*;
import java.awt.*;public class Solution extends JFrame {public Solution() {Container container = this.getContentPane();JTextArea textArea = new JTextArea(20, 30);textArea.setText("你好!!!");//scroll面板JScrollPane scrollPane = new JScrollPane(textArea);container.add(scrollPane);this.setVisible(true);this.setBounds(100, 100, 200, 200);this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);}public static void main(String[] args) {new Solution();}
}

在这里插入图片描述

5、按钮

6、列表

7、文本框


http://chatgpt.dhexx.cn/article/Iq3mVBuY.shtml

相关文章

GUI编程详解

1.简介 GUI的核心技术&#xff1a;Swing AWT GUI缺点&#xff1a; 1. 界面不美观2. 需要jre环境为什么要学习GUI&#xff1f; 可以写出自己想要的工具。实际工作中可能需要维护swing界面了解MVC和监听。 2.AWT 2.1相关知识 介绍&#xff1a; 包含了很多类和接口。用于图形…

python GUI入门(一入门介绍)

CUI: 命令行界面&#xff0c;conmmand line user Interface GUI: Graphical User Interface 1、python常用的主要跨平台图形用户界面库 1.1 Tkinter python自带的GUT库&#xff0c;“官方支持”&#xff0c;无须进行额外的下载安装&#xff0c;只要导入tkinter包即可使用。 …

GUI系统

1.gui概念 2.OSD实现 1.概念 OSD是on-screen display的简称&#xff0c;即屏幕菜单式调节方式。一般是按Menu键后屏幕弹出的显示器各项调节项目信息的矩形菜单&#xff0c;可通过该菜单对显示器各项工作指标包括色彩、模式、几何形状等进行调整&#xff0c;从而达到最佳的使用…

Python之GUI编程

一、常用的GUI库 Tkinter&#xff1a;tkinter&#xff08;Tk interface&#xff09;是Python的标准GUI库&#xff0c;支持跨平台的GUI程序开发&#xff0c;tkinter适合小型的GUI程序编写&#xff0c;也特别适合初学者学习GUI编程。wxPython&#xff1a;wxPython是比较流行的GU…

快速入门GUI-guider

快速入门GUI-guider&#xff08;本教程基于LVGL-v8.20&#xff09; 前言一、什么是GUI Guider&#xff1f;二、使用教程1.下载软件2.熟悉LVGL各种组件3.新建工程4.上手体验5.代码移植 完结撒花 前言 本教程要基于有移植好的LVGL—8.2.0的代码为基础&#xff0c;对于不懂怎么移…

Python-GUI界面设计(tkinter)

文章目录 前言一、函数方法二、导入三、窗口[1]. 创建[2]. 标题[3]. 大小[4]. 背景[5]. 删除 四、按钮[1]. 创建[2]. 放置&#xff08;绝对位置&#xff09;[3]. 放置&#xff08;相对位置&#xff09;[4]. 代码 五、单行文本[1]. 创建[2]. 代码 六、多行文本[1]. 创建[2]. 获取…

GUI编程

一&#xff0c;简介 GUI编程(Graphical User Interface)&#xff0c;即用户图形界面编程。 GUI淘汰原因&#xff1a;界面不够美观&#xff0c;需要jre环境。 学习GUI的好处&#xff1a;1&#xff0c;为后期学习MVC架构打好基础&#xff1b;2&#xff0c;了解监听&#xff1b;3&…

[java/初学者/GUI编程]GUI界面设计——界面组件类

前言 GUI&#xff0c;即图形用户界面&#xff0c;其英文全称是Graphics User Interface。 它是基于图形的界面,windows就是一个图形用户界面的操作系统,而DOS是基于命令提示符的操作系统,GUI编程就是编出一个图形用户界面的软件&#xff0c;它使用图形的方式&#xff0c;以菜…

QT GUI编程

QT GUI编程 一、QT基础1.1、QT简介1.2、QT应用范围1.3、开发环境1.4、QT软件下载安装教程1.5、QT内存管理1.6、第一个QT程序1.7、打印跟踪1.8、QT工程示例1.9、QT 图形编程1.10、信号与槽1.11、QT实现心仪的计算器1.12、将QT文件单独提取出来自由使用 二、窗口部件2.1、常用类介…

GUI(图形界面编程)

&#xff08;1&#xff09;GUI 与CLI GUI •Graphical User Interface(图形用户接口)。 •用图形的方式&#xff0c;来显示计算机操作的界面&#xff0c;这样更方便更直观。 CLI •Command line User Interface (命令行用户接口&#xff09; •就是常见的Dos命令行操作。 •需要…

GUI概述

GUI简介 GUI&#xff08;Graphics User Interface&#xff09;&#xff0c;中文名称为图形用户界面&#xff0c;是指采用图形方式显示的计算机操作用户界面&#xff0c;是计算机与其使用者之间的对话接口&#xff0c;是计算机系统的重要组成部分。 早期&#xff0c;电脑向用户提…

GUI(图形用户界面)

GUI全称是Graphical User Interface &#xff0c;即图形用户界面。GUI就是可以让用户直接操作的图形界面&#xff0c;包括窗口、菜单、按钮、工具栏和其他各种图形界面元素。 1.Swing概述 Swing是Java语言开发图形化界面的一个工具包。它以抽象窗口工具包&#xff08;AWT&…

虚拟存储技术

一.实现内存扩充的技术&#xff1a; &#xff08;1&#xff09;覆盖技术&#xff1a; 在程序运行中&#xff0c;在不同时刻把同一个存储区分配给不同程序段和数据段&#xff0c;实现存储区共享。适用于连续存储&#xff08;单一连续区分配&#xff0c;分区&#xff09; 如图B…

浅谈进程地址空间与虚拟存储空间

早期的内存分配机制 在早期的计算机中&#xff0c;要运行一个程序&#xff0c;会把这些程序全都装入内存&#xff0c;程序都是直接运行在内存上的&#xff0c;也就是说程序中访问的内存地址都是实际的物理内存地址。当计算机同时运行多个程序时&#xff0c;必须保证这些程序用…

虚拟存储器系统

技术来源 来源1&#xff1a;来源于服务器中虚拟化feature的实现&#xff0c;不同的程序需要共享一片硬件资源&#xff0c;包括计算资源与存储资源&#xff1b;如何有效且安全的共享存储器就是关键。来源2&#xff1a;消除有限的主存容量对大的程序的限制。远古时代&#xff0c…

虚拟存储器/虚拟内存

器为每个程序提供了一个大的、一致的、私有地址空间。 三个重要功能&#xff1a; 将主存看成是磁盘的高速缓存&#xff0c;在主存中只保存活动区域&#xff0c;并根据需要在磁盘和主存之间来回传送数据。为每个进程提供了一致的私有空间。保护每个进程的地址空间不被其他进程破…

5.虚拟存储管理

虚拟存储管理 文章目录 虚拟存储管理1.虚拟存储器概述1.1常规存储管理方式的特征和局部性原理1.2虚拟存储器的定义与特征1.3虚拟存储器的实现方法 2.请求分页存储管理方式2.1请求分页存储管理方式基本思想2.2请求分页中的硬件支持2.3内存分配策略和分配算法2.4调页策略 3.页面置…

(五)虚拟存储概念

1.需求背景 计算机系统内存空间不够用。 2.覆盖和交换 覆盖技术&#xff1a;把程序划分成功能独立的模块&#xff0c;将不会同时执行的模块共享同一块内存区域。 缺点&#xff1a;编程困难&#xff0c;增加了执行时间 交换技术&#xff1a;增加正在运行或需要运行的程序的内…

存储虚拟化技术的介绍

导读Java虚拟机内存的各个区域&#xff0c;以及这些区域的作用、服务对象以及其中可能产生的问题&#xff0c;作为大家的面试宝典。那一起来学习—存储虚拟化技术。 数据在整个计算机系统是最重要的一部分&#xff0c;也是最珍贵的。数据的存储一直是一个热议的话题&#xff0c…

进程地址空间与虚拟存储空间的理解

在进入正题前先来谈谈操作系统内存管理机制的发展历程&#xff0c;了解这些有利于我们更好的理解目前操作系统的内存管理机制。 一 早期的内存分配机制 在 早期的计算机中&#xff0c;要运行一个程序&#xff0c;会把这些程序全都装入内存&#xff0c;程序都是直接运行…