[ java GUI ] 图形用户界面
目录
1.GUI定义:
2.swing概述
3.JFrame 窗口组件
1.定义:
2.方法及代码:
3.JLabel 标签组件
1.定义:
2.插件的下载和使用
3.常用组件介绍:
4.聊天注册窗口举例:
4.事件处理
5.对话框
(1) showMessageDialog()----消息对话框
(2) showConfirmDialog()----确认对话框
6.内部类
1.定义:
2.意义:
3.匿名内部类:
4.举例:
1.GUI定义:
GUI(Graphical User Interface)图形用户界面 便于用户使用的操作界面
2.swing概述
Java的图形界面由各种不同类型的组件(Component)组成 , 而swing是一个为Java设计的图形界面工具包(javax.swing),包中有图形用户界面的各种组件
3.JFrame 窗口组件
1.定义:
自定义一个窗口类,继承JFrame,拥有窗口的功能
2.方法及代码:
package JavaGUIDemo;import javax.swing.*;
/*
自定义一个窗口类,继承JFrame,拥有窗口的功能*/
public class DemoFrame extends JFrame {//常用方法public DemoFrame(){//构造方法this.setTitle("我的窗口");//设置标题this.setSize(400,500);//设置窗口大小this.setLocationRelativeTo(null);//设置位置(居中)//this.setLocation(200,300);//设置位置(自定义位置)this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口,退出程序this.setResizable(false);//是否可以重置窗口大小this.setVisible(true);//显示窗口,放在最后一行//dispose();//释放关闭当前窗口}public static void main(String[] args) {new DemoFrame();}
}
3.JLabel 标签组件
1.定义:
标签是容纳文本和图标的控件,通常用来在界面中标识别的控件. 在窗口组件中,添加其他功能的组件
2.插件的下载和使用



3.常用组件介绍:

4.聊天注册窗口举例:
登录窗口:
package JavaGUIDemo;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;/*** @author yang*/
public class LoginFrame extends JFrame {public LoginFrame() {initComponents();}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:offlabel1 = new JLabel();label2 = new JLabel();passwordField = new JPasswordField();LoginField = new JTextField();LoginBtn = new JButton();ReBtn = new JButton();//======== this ========Container contentPane = getContentPane();contentPane.setLayout(null);//---- label1 ----label1.setText("\u767b\u5f55");label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | Font.BOLD, label1.getFont().getSize() + 5f));contentPane.add(label1);label1.setBounds(new Rectangle(new Point(50, 50), label1.getPreferredSize()));//---- label2 ----label2.setText("\u5bc6\u7801");contentPane.add(label2);label2.setBounds(60, 100, label2.getPreferredSize().width, 17);contentPane.add(passwordField);passwordField.setBounds(105, 95, 185, passwordField.getPreferredSize().height);contentPane.add(LoginField);LoginField.setBounds(105, 50, 180, LoginField.getPreferredSize().height);//---- LoginBtn ----LoginBtn.setText("\u767b\u5f55");contentPane.add(LoginBtn);LoginBtn.setBounds(new Rectangle(new Point(100, 170), LoginBtn.getPreferredSize()));//---- ReBtn ----ReBtn.setText("\u6ce8\u518c");contentPane.add(ReBtn);ReBtn.setBounds(new Rectangle(new Point(210, 170), ReBtn.getPreferredSize()));{// compute preferred sizeDimension preferredSize = new Dimension();for(int i = 0; i < contentPane.getComponentCount(); i++) {Rectangle bounds = contentPane.getComponent(i).getBounds();preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);}Insets insets = contentPane.getInsets();preferredSize.width += insets.right;preferredSize.height += insets.bottom;contentPane.setMinimumSize(preferredSize);contentPane.setPreferredSize(preferredSize);}pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:onthis.setTitle("欢迎登录");this.setLocationRelativeTo(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);LoginBtn.addActionListener(new ActionListener() {//匿名内部类 new+接口@Override//添加事件处理public void actionPerformed(ActionEvent e) {String loginfield=LoginField.getText();//获得文本框内容String passwordfield =passwordField.getText();//获得密码框内容if(loginfield.length()==0){//消息对话框JOptionPane.showMessageDialog(null,"账号不能为空","来自系统的提示",JOptionPane.INFORMATION_MESSAGE);return;}if(passwordfield.length()==0){JOptionPane.showMessageDialog(null,"密码不能为空");//JOptionPane.showMessageDialog(null,"不能为空");//确认对话框//JOptionPane.showConfirmDialog(null,"你确定要操作吗","消息",JOptionPane.OK_CANCEL_OPTION)return;}//预留数据库对接//连接服务器Socket//打开聊天窗口new ChatFrame();dispose();//释放关闭当前窗口}});ReBtn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new Reg();dispose();}});}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:offprivate JLabel label1;private JLabel label2;private JPasswordField passwordField;private JTextField LoginField;private JButton LoginBtn;private JButton ReBtn;// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:onpublic static void main(String[] args) {new LoginFrame();}
}
注册窗口:
package JavaGUIDemo;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;/*** @author yang*/
public class Reg extends JFrame {public Reg() {initComponents();}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:offlabel1 = new JLabel();label2 = new JLabel();label3 = new JLabel();textField1 = new JTextField();passwordField1 = new JPasswordField();passwordField2 = new JPasswordField();ReturnBtn = new JButton();ReBtn = new JButton();//======== this ========Container contentPane = getContentPane();contentPane.setLayout(null);//---- label1 ----label1.setText("\u7528\u6237\u540d");contentPane.add(label1);label1.setBounds(new Rectangle(new Point(75, 20), label1.getPreferredSize()));//---- label2 ----label2.setText("\u8bf7\u8f93\u5165\u5bc6\u7801");contentPane.add(label2);label2.setBounds(new Rectangle(new Point(60, 55), label2.getPreferredSize()));//---- label3 ----label3.setText("\u518d\u6b21\u8f93\u5165\u5bc6\u7801");contentPane.add(label3);label3.setBounds(50, 85, label3.getPreferredSize().width, 25);contentPane.add(textField1);textField1.setBounds(120, 15, 155, textField1.getPreferredSize().height);contentPane.add(passwordField1);passwordField1.setBounds(135, 50, 140, passwordField1.getPreferredSize().height);contentPane.add(passwordField2);passwordField2.setBounds(140, 85, 135, passwordField2.getPreferredSize().height);//---- ReturnBtn ----ReturnBtn.setText("\u8fd4\u56de");contentPane.add(ReturnBtn);ReturnBtn.setBounds(new Rectangle(new Point(90, 140), ReturnBtn.getPreferredSize()));//---- ReBtn ----ReBtn.setText("\u6ce8\u518c");contentPane.add(ReBtn);ReBtn.setBounds(new Rectangle(new Point(215, 140), ReBtn.getPreferredSize()));{// compute preferred sizeDimension preferredSize = new Dimension();for(int i = 0; i < contentPane.getComponentCount(); i++) {Rectangle bounds = contentPane.getComponent(i).getBounds();preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);}Insets insets = contentPane.getInsets();preferredSize.width += insets.right;preferredSize.height += insets.bottom;contentPane.setMinimumSize(preferredSize);contentPane.setPreferredSize(preferredSize);}pack();setLocationRelativeTo(getOwner());// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:onsetVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLocationRelativeTo(null);ReturnBtn.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {new LoginFrame();dispose();}});}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:offprivate JLabel label1;private JLabel label2;private JLabel label3;private JTextField textField1;private JPasswordField passwordField1;private JPasswordField passwordField2;private JButton ReturnBtn;private JButton ReBtn;// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:onpublic static void main(String[] args) {new Reg();}
}
聊天窗口:
/** Created by JFormDesigner on Sun Mar 02 16:17:47 CST 2025*/package JavaGUIDemo;import java.awt.*;
import javax.swing.*;/*** @author yang*/
public class ChatFrame {public ChatFrame() {initComponents();}private void initComponents() {// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents @formatter:offChat = new JFrame();scrollPane1 = new JScrollPane();chatFrame = new JTextArea();message = new JLabel();scrollPane2 = new JScrollPane();inputfield = new JTextArea();sendbtn = new JButton();//======== Chat ========{Chat.setTitle("\u804a\u5929\u7a97\u53e3");Container ChatContentPane = Chat.getContentPane();ChatContentPane.setLayout(null);//======== scrollPane1 ========{scrollPane1.setViewportView(chatFrame);}ChatContentPane.add(scrollPane1);scrollPane1.setBounds(0, 0, 440, 255);//---- message ----message.setText("text");ChatContentPane.add(message);message.setBounds(445, 0, 115, 255);//======== scrollPane2 ========{scrollPane2.setViewportView(inputfield);}ChatContentPane.add(scrollPane2);scrollPane2.setBounds(5, 260, 435, 85);//---- sendbtn ----sendbtn.setText("\u53d1\u9001");ChatContentPane.add(sendbtn);sendbtn.setBounds(new Rectangle(new Point(450, 290), sendbtn.getPreferredSize()));{// compute preferred sizeDimension preferredSize = new Dimension();for(int i = 0; i < ChatContentPane.getComponentCount(); i++) {Rectangle bounds = ChatContentPane.getComponent(i).getBounds();preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);}Insets insets = ChatContentPane.getInsets();preferredSize.width += insets.right;preferredSize.height += insets.bottom;ChatContentPane.setMinimumSize(preferredSize);ChatContentPane.setPreferredSize(preferredSize);}Chat.setLocationRelativeTo(Chat.getOwner());Chat.setLocationRelativeTo(null);Chat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Chat.setVisible(true);}// JFormDesigner - End of component initialization //GEN-END:initComponents @formatter:on}// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables @formatter:offprivate JFrame Chat;private JScrollPane scrollPane1;private JTextArea chatFrame;private JLabel message;private JScrollPane scrollPane2;private JTextArea inputfield;private JButton sendbtn;// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:onpublic static void main(String[] args) {new ChatFrame();}
}
注意 : 本次只是单独做出窗口,并没有进行关联
4.事件处理
事件源产生事件并把它送至监听器,监听器一直等待,直到接收到一个事件,一旦事件被接收,监听器将处理这些事件

//添加事件处理 LoginBtn.addActionListener(new ActionListener() {//匿名内部类 new+接口/抽象类@Overridepublic void actionPerformed(ActionEvent e) {String loginfield=LoginField.getText();//获得文本框内容String passwordfield =passwordField.getText();//获得密码框内容if(loginfield.length()==0){//消息对话框JOptionPane.showMessageDialog(null,"账号不能为空","来自系统的提示",JOptionPane.INFORMATION_MESSAGE);return;}if(passwordfield.length()==0){JOptionPane.showMessageDialog(null,"密码不能为空");//JOptionPane.showMessageDialog(null,"不能为空");//确认对话框//JOptionPane.showConfirmDialog(null,"你确定要操作吗","消息",JOptionPane.OK_CANCEL_OPTION)return;}//预留数据库对接//连接服务器Socket//打开聊天窗口new ChatFrame();dispose();//释放关闭当前窗口} });
5.对话框
(1) showMessageDialog()----消息对话框
ERROR_MESSAGE 错误消息提示
INFORMATION_MESSAGE 信息提示
WARNING_MESSAGE 警告提示
QUESTION_MESSAGE 问题提示
PLAIN_MESSAGE 简洁提示
(2) showConfirmDialog()----确认对话框
DEFAULT_OPTION 默认选项
YES_NO_OPTION 是/否选项
YES_NO_CANCEL_OPTION 是/否/取消选项
OK_CANCEL_OPTION 确定/取消
6.内部类
1.定义:
有些功能在外部用不到,所以把一些实现, 定义在一个内部中封装起来,并且内部类还可以直接使用外部类中的成员; 为了简化写法,不用创建接口/抽象类的实现类,直接new+抽象类/接口 创建匿名内部类,减少内部类的创建
注意: 内部类仍然是一个独立的类,在编译之后内部类会被编译成独立的.class文件,但是前面冠以外部类的类名和$符号
地址 : D:\ideaproject2402\javaGUI\out\production\javaGUI\OutClass (out是编译后的文件)
2.意义:
1.实现功能的封装
2.实现多继承(son类需要继承father和mother类,可定义两个内部类,分别继承father和mother)
3.匿名内部类:
定义 : 是一种特殊的局部内部类,它是通过匿名类实现接口。
4.举例:
addActionListener()内部,需传入一个实现了ActionListener()接口的实现类,进行事件处理
方案一: 创建一个正常的类去实现ActionListener()接口,在这个类里面去写执行相应程序的代码,但 是作为一个独立的类,想要使用其他类的成员需要继承 而且一些类使用不到 麻烦且多余
方案二: 创建内部类,把要执行事件处理的类定义在窗口类的内部.不对外暴露(只服务于当前类)且 内部类可以直接使用外部类中的成员. 但是在程序中只使用一次时也不划算
方案三: 创建匿名内部类new+ 接口/抽象方法(的名称) 在只使用一次的场景下,不需要我们创建一个 类/内部类实现接口, 为抽象类和接口提供匿名内部类, 少创建一个类,减少代码冗余
安装插件时
C盘--当前用户目录--AppData(如果找不到可能在隐藏目录里面,点击查看--显示--隐藏目录)--Roaming--JetBrains--IntelliJIdea2023.3--plugins--JFormDesigner--lib
C:\Users\yang\AppData\Roaming\JetBrains\IntelliJIdea2023.3\plugins\JFormDesigner\lib