package com.shiyanlou.view;
//This declares the package name,
//which is com.shiyanlou.view. This helps in organizing your classes.
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
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.JPanel;
import javax.swing.border.EmptyBorder;
/*
* These are import statements that bring in various classes from the Java standard library needed for the GUI application.
* These classes handle event dispatching(事件调度),
* font styling(字体调整), event handling(事件处理), and Swing components like buttons, labels, and panels.
*/
/*
* This declares the IndexGUI class, which extends JFrame,
* meaning IndexGUI is a type of JFrame
* and
* inherits all its properties and methods.
*/
public class IndexGUI extends JFrame {
//自定义IndexGUI继承JFrame类
//声明面板
private JPanel contentPane;
//创建JFrame的类对象声明
private static IndexGUI frame;//属于类
/*
* contentPane is a JPanel that will act as the main panel of the frame.
frame is a static reference to the IndexGUI object,
allowing it to be accessed within static methods.
*/
public static void main(String[] args) {
init(); //调用初始化方法
}
/*
* The main method is the entry point of the Java application.
* It calls the init method to initialize the GUI.
*/
//初始化方法
/*
* 1)init is a static method that uses EventQueue.invokeLater to ensure that the creation and updates to the GUI are performed on the Event Dispatch Thread, which is the standard way to create and interact with GUI components in Swing.
(to put it simply, everyone will use EventQueue.invokeLater to
handle event-dispatching if he want to create and interact with
GUI components in Swing)
2) Inside the run method of Runnable, it instantiates the IndexGUI frame and makes it visible.
3) Any exceptions are caught and printed to the stack trace.
*
*/
public static void init() {
EventQueue.invokeLater(new Runnable(){
public void run() {
try {
frame = new IndexGUI(); //实例化frame
//设
//设置窗体可见性
frame.setVisible(true);//
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
//IndexGUI ---构造方法
/*
* This is the constructor for IndexGUI, which sets up the frame and its components.
*/
public IndexGUI() {
//setTitle sets the title of the window.
setTitle("KnowYou");//设置窗体标题
//设置默认关闭方式,点击窗体关闭按关闭窗体
//setDefaultCloseOperation specifies the behavior when the user closes the window.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds sets the position and size of the window.
setBounds(100,100,650,400);
//contentPane is instantiated, and its border is set to provide padding.
contentPane = new JPanel();实例化面板
//设置面板大小,位置
contentPane.setBorder(new EmptyBorder(5,5,5,5));
//frame添加面板
//setContentPane adds the contentPane to the frame.
setContentPane(contentPane);
//面板设置布局为null,不可省略。否则页面布局将会杂乱。
//contentPane.setLayout(null) sets the layout manager to null, meaning absolute positioning is used.
contentPane.setLayout(null);
//A JLabel is created, positioned, styled, and added to contentPane.
JLabel lblNewLabel = new JLabel("Welcome to use KnowYou");//标题
lblNewLabel.setBounds(132, 74, 386, 35);
lblNewLabel.setFont(new Font("Ubuntu", Font.BOLD | Font.ITALIC,30));
contentPane.add(lblNewLabel);
//登录按钮
//A JButton for login is created,
//and MouseListener and KeyListener are added to handle mouse clicks and key presses.
JButton login = new JButton("Login");
//登录按钮鼠标事件,当鼠标被点击时调用
login.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
event_Login();//登录事件方法
}
});
//增加键盘事件
login.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
event_Login();//登录事件方法
}
}
});
//The button is positioned and added to contentPane.
login.setBounds(65, 263, 124, 45);
contentPane.add(login);
//注册按钮
//Another JButton for registration is created with similar event handling and positioning.
JButton register = new JButton("Sign up");
//注册鼠标事件
register.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
event_register();//注册事件方法
}
});
//注册按钮键盘事件
register.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
event_register();//注册事件方法
}
}
});
register.setBounds(489, 263, 109, 45);
contentPane.add(register);
}
//对登录和注册事件进行私有方法封装
/*
* These private methods handle the events for login and registration buttons.
event_Login hides the current window and opens the LoginGUI.
event_register hides the current window and opens the RegisterGUI.
*/
private void event_Login() {
setVisible(false);
new LoginGUI().loginGUI();
}
private void event_register() {
setVisible(false);
new RegisterGUI().registerGUI();
}
}
[package-view] IndexGUI.java-自用
于 2024-06-12 20:29:55 首次发布