当前位置: 首页 > news >正文

[ 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.匿名内部类:

       定义 : 是一种特殊的局部内部类,它是通过匿名类实现接口。

       写法 : new 接口名称/抽象类名称() {
                       重写抽象方法;
                  }
       意义 : 在开发中,当一个接口/抽象类的方法的某个实现方式在程序中可能只需要执行一次,但                   为了使用它,我们需要创建它的实现类。此时可以使用匿名内部类的方式,可以无需创                   建新的类,减少代码冗余

   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

http://www.dtcms.com/a/317656.html

相关文章:

  • 【软考系统架构设计师备考笔记4】 - 英语语法一篇通
  • ctfshow_vip题目限免-----SVN漏洞,git泄露
  • Git Cherry-Pick 指南
  • Leetcode——菜鸟笔记1
  • Git 分支管理:从新开发分支迁移为主分支的完整指南
  • 鸿蒙app 开发中 全局弹窗类的封装 基于PromptAction
  • C#之基础语法
  • 机器学习之朴素贝叶斯
  • Suno API V5模型 php源码 —— 使用灵感模式进行出创作
  • 基于PHP的论坛社交网站系统开发与设计
  • 排序算法详解
  • 媒体资产管理系统和OCR文字识别的结合
  • Ethereum: L1 与 L2 的安全纽带, Rollups 技术下的协作与区别全解析
  • 解决启动docker报错Cannot connect to the Docker daemon问题
  • 阿里 Qwen-Image:开源 20B 模型引领图像生成新纪元,中文渲染超越 GPT-4o!
  • 数据结构与算法的认识
  • 手动开发一个TCP服务器调试工具(二):无界面 TCP 通信服最小实现
  • ETF期权分仓的风险如何管理?
  • 基于Hadoop的股票大数据分析可视化及多模型的股票预测研究与实现
  • 四十、【高级特性篇】接口用例数据驱动:引入随机变量与动态数据生成
  • 生成式模型 ?判别式模型?用【猫狗分类器】帮助理解!
  • 【网络安全】入侵检测系统 Suricata 概述 | IDS
  • 2025年大语言模型与多模态生成工具全景指南(V2.0)
  • PyCharm vs. VSCode 到底哪个更好用
  • 5个数据库 存储系统精选 | C/C++ 项目深度解析
  • 支持向量机(SVM)算法依赖的数学知识详解
  • 深度模拟用户行为:用Playwright爬取B站弹幕与评论数据
  • 使用Java爬取xxx律师协会网站上公开的律所信息并导出到Excel
  • 服务器——“查询不到显卡驱动,且输入nvidia-smi报错”的解决办法
  • 时序数据库的发展现状与未来趋势