[package-view] RegisterGUI.java-自用

news2024/11/26 10:44:57

java GUI :

frame---> panel --> components[button/输入框/标签]

JFrame-->JPanel---> [JButton/JTextField/JLabel]

/*
 * This code sets up a registration window using Swing. 
 * The window includes input fields for the user's name, ID,
 *  and password, 
 *  as well as buttons for submitting the registration or going back to the main window. 
 *  Event listeners handle button clicks, performing validation and displaying appropriate messages. 
 *  The layout is managed using absolute positioning with null layout, 
 *  ensuring precise control over component placement.
 * 
 */
package com.shiyanlou.view;
//Declares the package name, com.shiyanlou.view, 
//which organizes the classes into a namespace.

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.shiyanlou.util.Register;
/*
 * Imports necessary classes 
 * from the Java standard library 
 * and 
 * Register from a custom utility package. 
 * These imports include classes 
 * for
 *  event handling, GUI components, and layouts.
 */

//Class Declaration and Variables
//RegisterGUI extends JFrame, making it a type of window.
public class RegisterGUI extends JFrame{
	
	//serialVersionUID is a unique identifier for the class, used during deserialization.
	private static final long serialVersionUID =3250371445038102261L;
	//contentPane is a JPanel that acts as the main panel of the frame.
	private JPanel contentPane;
	//nametext, IDtext, and passwdtext are JTextField components for user input.
	private JTextField nametext;//name输入框
	private JTextField IDtext; //ID输入框
	private JTextField passwdtext; //密码输入框
	
	/**
     * Launch the application.
     */
	//Main Method and Event Dispatch Thread
	public void registerGUI() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegisterGUI frame = new RegisterGUI();
					frame.setVisible(true);
				}catch(Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		/*
		 * In Java, the method public void registerGUI is not a constructor, 
		 * even though it shares part of its name with the class.
		 *  The key difference is that it has a return type (void), while constructors do not have any return type (not even void). 
		 *  Constructors are special methods used to initialize objects and they must have the same name as the class without a return type.

		 * Role and Purpose of registerGUI
Naming Convention:
 Although the method name registerGUI includes the class name RegisterGUI, it is still a regular method because it has a return type (void). This is perfectly legal in Java. Naming a method similar to the class name can be a convention used to indicate that the method is closely related to the class's primary functionality, but it is not required.

GUI Initialization: 
The purpose of registerGUI is to launch the registration GUI. It schedules a job for the event-dispatching thread using EventQueue.invokeLater, ensuring that the creation of the GUI and its components happens in the correct thread.

Runnable Interface: 
Inside the invokeLater method, an anonymous inner class implementing the Runnable interface is provided. The run method of this interface contains code to create an instance of RegisterGUI and make it visible.

Error Handling: 
The try-catch block is used to handle any exceptions that might occur during the creation and display of the GUI.
		 */
	}
	
	 /**
     * Create the frame.
     */
	//Constructor and Frame Setup
	
	public RegisterGUI() {
		/*
		 * The constructor sets the default close operation for the frame, window size, and border for contentPane.
The setContentPane method adds the contentPane to the frame, and setLayout(null) sets the layout manager to null for absolute positioning.
		 */
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,650,400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5,5,5,5));
		
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		//设置提示姓名输入标签
		//Lables for Input Fields
		//Creates JLabel components to prompt the user for their name, ID, and password. 
		//Each label is positioned and added to the contentPane.
		JLabel namelabel = new JLabel("Please input user name");
		namelabel.setBounds(102, 91, 151, 23);
		contentPane.add(namelabel);
		
		设置提示ID输入标签
		JLabel IDlabel = new JLabel("Please input user ID");
		IDlabel.setBounds(102, 160, 151, 23);
		contentPane.add(IDlabel);
		
		//设置提示密码输入标签
		JLabel passwdlabel = new JLabel("Please input user password");
		passwdlabel.setBounds(102, 224, 163, 23);
		contentPane.add(passwdlabel);
		
		//Text Fields for User Input
		/*
		 * Creates JTextField components for the user to input their name, ID, and password. 
		 * Each field is positioned and added to the contentPane.
		 */
		nametext = new JTextField();//普通文本输入框
		nametext.setBounds(271, 92, 92, 21);//设置位置及大小
		
		contentPane.add(nametext);
		nametext.setColumns(10);//设置长度
		
		//ID
		IDtext = new JTextField();
		IDtext.setBounds(271, 161, 92, 21);
		contentPane.add(IDtext);
		IDtext.setColumns(8);
		
		//密码
		passwdtext = new JTextField();
		passwdtext.setBounds(271, 225, 92, 21);
		contentPane.add(passwdtext);
		passwdtext.setColumns(10);
		
		//注册按钮
		//Register Button and Event Handling
		//
		/*JButton Creation
		 * JButton: This creates a new button labeled "Sign up". 
		 * In Swing, JButton is used to create clickable buttons in the GUI.
		 */
		JButton register = new JButton("Sign up");
		
		//注册按钮鼠标点击事件
		//Adding Mouse Click Event Listener
		/*
		 * Creates a JButton for registration and adds a MouseListener to handle mouse clicks.
When the button is clicked, it retrieves the input from the text fields and performs validation using methods from the Register utility class.
If validation is successful, it registers the user and shows a success message. Otherwise, it shows an error message.
The button is positioned and added to the contentPane.
		 */
		/*
		 * addMouseListener: 
		 * This method is used to add an event listener to the button. When the button is clicked, the specified MouseAdapter will handle the event.

MouseAdapter:
 This is an abstract adapter class for receiving mouse events. The mouseClicked method is overridden to provide the desired behavior when the button is clicked.
		 */
		register.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				/*
				 * Let's break down each part of the mouseClicked method:

1.Get user inputs:

String name = nametext.getText();: Retrieves the text entered in the nametext field.
String ID = IDtext.getText();: Retrieves the text entered in the IDtext field.
String passwd = passwdtext.getText();: Retrieves the text entered in the passwdtext field.

2.Validate ID:

if (Register.checkID(ID) == null): Calls a method checkID from the Register class to validate the ID. If the ID is valid (i.e., checkID returns null), the password is then validated.

3.Validate password:

if (Register.checkPasswd(passwd) == null): Calls a method checkPasswd from the Register class to validate the password. If the password is valid (i.e., checkPasswd returns null), the registration proceeds.

4.Register user and get response message:

String srt = Register.register(name, passwd, ID);: Calls the register method from the Register class, passing the name, password, and ID. It receives a response message, presumably indicating success.

5.Show success message:

JOptionPane.showMessageDialog(contentPane, srt, "information", JOptionPane.PLAIN_MESSAGE);: Displays a message dialog showing the success message.

6.Hide current window:

setVisible(false);: Makes the current window invisible.

7.Show main window:

new IndexGUI().init();: Creates a new instance of IndexGUI and calls its init method to display the main window.

8.Show password validation error:

JOptionPane.showMessageDialog(contentPane, Register.checkPasswd(passwd), "ERROR", JOptionPane.ERROR_MESSAGE);: If the password is invalid, it displays an error message with the validation error.

9.Show ID validation error:

JOptionPane.showMessageDialog(contentPane, Register.checkID(ID), "ERROR", JOptionPane.ERROR_MESSAGE);: If the ID is invalid, it displays an error message with the validation error.
				 */
				//这里的事件暂且不处理,日后我们将会完善方法。
				String name = nametext.getText();//得到name
				String ID = IDtext.getText();//得到ID
				String passwd = passwdtext.getText();//得到密码
				
				//如果检测ID返回为null
				if(Register.checkID(ID) == null) {
					//如果检测密码返回为null
					if(Register.checkPasswd(passwd) == null) {
						//注册信息,并且得到返回信息
						String srt = Register.register(name, passwd, ID);
						
						//提示框,注册成功
						JOptionPane.showMessageDialog(contentPane, srt,"information",JOptionPane.PLAIN_MESSAGE);
						
						//隐藏当前窗体
						setVisible(false);
						// 返回首页
						new IndexGUI().init();
						}else {
							//提示框,输出错误信息
							JOptionPane.showMessageDialog(contentPane,Register.checkPasswd(passwd),"ERROR",JOptionPane.ERROR_MESSAGE);
							
					}
				}else {
					//提示框,输出错误信息
					JOptionPane.showMessageDialog(contentPane,Register.checkID(ID),"ERROR",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		//Setting Button Position and Adding to Panel
		/*
		 * setBounds: This method sets the position and size of the button. Here, the button's position is (321, 305) and its size is 93 pixels wide and 23 pixels tall.
add: This method adds the button to the contentPane (the main panel of the frame).
		 */
		register.setBounds(321,305,93,23);
		contentPane.add(register);
		
		/*
		 * Summary
Button Creation: A "Sign up" button is created.
Event Listener: A mouse click event listener is added to the button to handle registration when clicked.
Event Handling: When clicked, the button retrieves user input, validates the ID and password, registers the user if valid, displays messages, and navigates to the main window if registration is successful. If validation fails, appropriate error messages are shown.
Positioning: The button is positioned on the panel and added to it.
		 */
		
		
		/
		
		//Back Button and Event Handling
		JButton back = new JButton("BACK");//返回按钮
		back.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				IndexGUI.init();//创建首页
				setVisible(false);//当年页面不可见
			}
		});
		
		back.setBounds(531, 305, 93, 23);
		contentPane.add(back);
		/*
		 * Creates a JButton for going back to the main window and adds a MouseListener to handle mouse clicks.
When the button is clicked, it initializes the IndexGUI and hides the current window.
The button is positioned and added to the contentPane.
		 */
		
		
		
		//Welcome Title and Additional Labels
		/*
		 * Creates a JLabel for the welcome title, sets its font, and positions it.
Adds additional JLabel components to provide hints about the ID and password requirements.
Each label is positioned and added to the contentPane.
		 */
		
		JLabel label = new JLabel("Welcome to use KnowYou");//欢迎标题
		label.setFont(new Font("Ubuntu", Font.BOLD | Font.ITALIC,30));
		label.setBounds(143, 26, 374, 35);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("(There are 1 to 8 numbers)");
		lblNewLabel.setBounds(373, 164, 163, 15);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("(There are 6 to 15 numbers)");
        lblNewLabel_1.setBounds(373, 228, 163, 15);
        contentPane.add(lblNewLabel_1);
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1845600.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue3 自定义指令-积分埋点

自用笔记,内容可能不全 // package.json "sunshine-track": "^1.0.0-beta.2",// track index.jsimport Track from sunshine-trackconst options {projectKey: test-project, // 项目的keyuserId: digger, // 用户idreport: {url: http://exam…

uniapp app打开微信客服

按钮触发 uni.share({provider: "weixin",openCustomerServiceChat: true,customerUrl: https://work.weixin.qq.com/kfid/*************, //企业微信地址corpid: ww13edaa**********, //企业idsuccess: (res) > {console.log("success:" JSON.string…

Redis精要

一、什么是缓存击穿、缓存穿透、缓存雪崩? 缓存穿透 【针对大量非法访问的请求,缓存中没有,直接访问DB】 缓存穿透指的查询缓存和数据库中都不存在的数据,这样每次请求直接打到数据库,就好像缓存不存在 一样。 对于系…

【经验分享】Ubuntu24.04安装微信

【经验分享】Ubuntu24.04安装微信(linux官方2024universal版) 文章如下,22.04和24.04微信兼容 【经验分享】Ubuntu22.04安装微信(linux官方2024universal版) 实测Ubuntu24.04LTS版本可以兼容。

cenots 出现 curl 外网地址很慢,或者微信授权很慢

用 curl 访问链接,很慢才显示接口、或者微信授权很慢, 微信授权,很慢才授权成功,平均延时 5s 可能是 dns配置问题,直接修改 sudo vim /etc/resolv.conf 的 nameserver 改为 114.114.114.114 即可 其他…

锂电池寿命预测 | Matlab基于ARIMA的锂电池寿命预测

目录 预测效果基本介绍程序设计参考资料 预测效果 基本介绍 锂电池寿命预测 | Matlab基于ARIMA的锂电池寿命预测 NASA数据集,B0005号电池,选择前110个数据训练,后58个数据测试预测。程序包含去趋势线、差分、平稳化及AIC准则判定p和q。命令窗…

DSP28335课设:音乐流水灯的设计

本题目为音乐流水灯的设计,目的是熟练掌握DSP定时器的控制、中断系统的应用以及程序的编写调试。 1、让DSP发出不同的音乐音调; 2、存储一个简单音乐(自选); 3、音乐的音调对应不同的灯 4、启动开关按下时&…

C++Muduo网络库初探

Muduo初探 Muduo网络库简介 Muduo是由【陈硕】大佬个人开发的TCP网络编程库,基于Reactor模式,提供了高效的事件驱动网络编程框架,有助于快速搭建高性能的网络服务端。 什么是Reactor模式? I/O多路复用 在网络I/O中&#xff0…

eclipse中没有SERVER的解决办法(超详细)

将 Tomcat 和 Eclipse 相关联时,Eclipse有的版本发现 发现eclipse->【Window】->【Preferences】里没有【server】从而配置不了Runtime Environment。所以需要通过eclipse进行安装。 通过我个人的经验下面给出解决办法: 一、获取 Eclipse版本 点击…

性能测试并发量评估新思考:微服务压力测试并发估算

性能测试并发量评估新思考 相信很多人在第一次做压力测试的时候,对并发用户数的选择一直有很多的疑惑,那么行业内有一些比较通用的并发量的计算方法,但是这些方法在如今微服务的架构下多少会有一些不适合,下面的文章我们对这些问题…

JS【数组】交集、差集、补集、并集

var a [1,2,3,4,5] var b [2,4,6,8,10]var sa new Set(a); var sb new Set(b); // 交集 let intersect a.filter(x > sb.has(x)); // 差集 let minus a.filter(x > !sb.has(x)); // 补集 let complement [...a.filter(x > !sb.has(x)), ...b.filter(x > !sa…

MicroBlaze IP核中Local Memory Bus (LMB)接口描述

LMB(Local Memory Bus)是一种同步总线,主要用于访问FPGA上的块RAM(Block RAM,BRAM)。LMB使用最少的控制信号和一个简单的协议,以保证块RAM能在一个时钟周期内被存取。所有的LMB信号都是高电平有…

[深度学习]--分类问题的排查错误的流程

原因复现: 原生的.pt 好使, 转化后的 CoreML不好使, 分类有问题。 yolov8 格式的支持情况 Format Argument Suffix CPU GPU 0 PyTorch - .pt True True 1 Tor…

Unet已死,Transformer当立!详细解读基于DiT的开源视频生成大模型EasyAnimate

Diffusion Models视频生成-博客汇总 前言:最近阿里云PIA团队开源了基于Diffusion Transformer结构的视频生成模型EasyAnimate,并且提出了专门针对视频的slice VAE,对于目前基于Unet结构的视频生成最好如SVD形成了降维打击,不论是生…

快消品经销商的仓库管理,有哪些是必须注意的事项?

快消品经销商仓库管理是一个复杂而关键的过程,它涉及到产品的存储、保管、发货以及库存控制等多个环节。一个高效的仓库管理系统不仅有助于减少成本,提高运营效率,还能确保产品质量和满足客户需求。以下是一些快消品经销商在仓库管理过程中需…

JDBC之API(DriverManager)详解

之前在 JDBC 的快速入门写代码的时候,遇到了很多的API。这篇博客主要学习一些API。 目录 一、API(介绍) 二、JDBC之API——DriverManager (1)DriverManager (获取 Connection 的连接对象) 1、…

玩转热门游戏,选对系统是关键!游戏专用电脑系统在这里!

如果我们给电脑安装上游戏专用系统,那么就能体验到更加流畅、稳定的游戏运行环境,享受沉浸式的游戏体验。但是,许多新手用户不知道去哪里才能下载到玩游戏专用的电脑系统?接下来小编给大家分享玩游戏专用电脑系统,这些…

Windows应急响应靶机 - Web2

一、靶机介绍 应急响应靶机训练-Web2 前景需要:小李在某单位驻场值守,深夜12点,甲方已经回家了,小李刚偷偷摸鱼后,发现安全设备有告警,于是立刻停掉了机器开始排查。 这是他的服务器系统,请你…

【YOLOv5/v7改进系列】引入特征融合网络——ASFYOLO

一、导言 ASF-YOLO结合空间和尺度特征以实现精确且快速的细胞实例分割。在YOLO分割框架的基础上,通过引入尺度序列特征融合(SSFF)模块来增强网络的多尺度信息提取能力,并利用三重特征编码器(TFE)模块融合不同尺度的特征图以增加细节信息。此外&#xff…

尚硅谷大数据技术ClickHouse教程-笔记01【ClickHouse单机安装、数据类型】

视频地址:一套上手ClickHouse-OLAP分析引擎,囊括Prometheus与Grafana_哔哩哔哩_bilibili 01_尚硅谷大数据技术之ClickHouse入门V1.0 尚硅谷大数据技术ClickHouse教程-笔记01【ClickHouse单机安装、数据类型】尚硅谷大数据技术ClickHouse教程-笔记02【表引…