package code.student.view;
import code.handler.LoginViewHandler;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
public class LoginView extends JFrame{
JLabel nameLabel = new JLabel("学生成绩管理项目", JLabel.CENTER); // 设置标题为水平居中
SpringLayout springLayout = new SpringLayout();
JPanel centerPanel = new JPanel(springLayout); // 设置面板的布局为弹簧布局
JLabel userNameLabel = new JLabel("用户名:");
JTextField userTxt = new JTextField();
JLabel pwdLabel = new JLabel("密 码:");
JPasswordField pwdField = new JPasswordField();
JButton loginBtn = new JButton("登录");
JButton resetBtn = new JButton("重置");
JLabel register = new JLabel("立即注册");
// 定义系统托盘
SystemTray systemTray; // SystemTray类表示桌面的系统托盘
// 定义托盘对象
TrayIcon trayIcon; // TrayIcon对象表示可以添加到系统托盘的托盘图标
LoginViewHandler loginHandler;
public LoginView() {
super("学生成绩管理项目"); // 设置项目标题
loginHandler = new LoginViewHandler(this);
// 获取内容面板
// 父类有一个获取ContentPane对象的方法,将获取到的东西赋给contentPane
Container contentPane = getContentPane();
// 设置标题字体
nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
// 设置宽高
nameLabel.setPreferredSize(new Dimension(0, 80));
// 设置中间区域的字体
Font centerFont = new Font("楷体", Font.PLAIN, 20);
userNameLabel.setFont(centerFont);
userTxt.setPreferredSize(new Dimension(200, 30));
pwdLabel.setFont(centerFont);
pwdField.setPreferredSize(new Dimension(200, 30));
loginBtn.setFont(centerFont);
resetBtn.setFont(centerFont);
register.setFont(centerFont);
// 把组件加入面板
/*
this.centerPanel.add();
可以理解成:
A a = new A(); --> 即JFrame
ContentPane contentPane = new ContentPane();
contentPane = a.getContentPane();
contentPane.add();
*/
this.centerPanel.add(userNameLabel);
this.centerPanel.add(userTxt);
this.centerPanel.add(pwdLabel);
this.centerPanel.add(pwdField);
loginBtn.addActionListener(loginHandler);
this.centerPanel.add(register);
register.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int clickCount = e.getClickCount();
if (clickCount == 1) {
// 匿名内部类访问外边的setExtendedState(int state)方法,设置此窗体的状态为普通(即原本的大小)
new RegisterView();
setVisible(false);
}
}
});
// 增加按键事件
loginBtn.addKeyListener(loginHandler);
this.centerPanel.add(loginBtn);
resetBtn.addActionListener(loginHandler);
this.centerPanel.add(resetBtn);
userTxt.setToolTipText("请输入正确的用户名");
pwdField.setToolTipText("请输入正确的密码");
// 弹簧布局
layoutCenter();
// 把标题和中间面板添加到内容面板
contentPane.add(nameLabel, BorderLayout.NORTH);
contentPane.add(this.centerPanel, BorderLayout.CENTER);
// contentPane.add(register, BorderLayout.SOUTH);
// 判断当前系统是否支持系统托盘
if (SystemTray.isSupported()) {
// 初始化系统托盘
systemTray = SystemTray.getSystemTray();
URL imgUrl = LoginView.class.getClassLoader().getResource("tray.jpg");
// 初始化托盘对象
trayIcon = new TrayIcon(new ImageIcon(imgUrl).getImage());
// 设置托盘图片大小自动缩放
trayIcon.setImageAutoSize(true);
// 添加事件,增加最小化时销毁资源
/*
void windowIconified(WindowEvent e)窗口从正常状态变为最小化状态时调用
*/
this.addWindowListener(new WindowAdapter() {
@Override
public void windowIconified(WindowEvent e) {
try {
// 把自定义的Icon托盘加入到系统托盘中(会抛出异常)
systemTray.add(trayIcon);
} catch (AWTException e1) {
e1.printStackTrace();
}
// 因为它是匿名内部类,就要LoginView.this.dispose()
LoginView.this.dispose();
}
});
// 托盘事件监听(鼠标监听)
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// 先获取鼠标点击的次数存入变量中
int clickCount = e.getClickCount();
if (clickCount == 1) {
// 匿名内部类访问外边的setExtendedState(int state)方法,设置此窗体的状态为普通(即原本的大小)
LoginView.this.setExtendedState(JFrame.NORMAL);
}
systemTray.remove(trayIcon);
// 因为前面已经销毁这个界面,所以需要重新设置显示界面为true
LoginView.this.setVisible(true);
}
});
}
// 设置loginBtn为默认按钮
getRootPane().setDefaultButton(loginBtn);
// 自定义图标
// getClassLoader()返回该类的类加载器
// getResource(String name)查找带有给定名称的资源
// 此处应该有用到注解反射知识
/* 实际上java的每个类被编译成.class文件的时候,java虚拟机(叫jvm)会自动为这个类生成一个类对象,
这个对象保存了这个类的所有信息(成员变量,方法,构造器等),
以后这个类要想实例化(也就是创建类的实例或创建类的对象)那么都要以这个class对象为蓝图(或模版)来创建这个类的实例。
*/
URL imgUrl = LoginView.class.getClassLoader().getResource("image1.jpg");
setIconImage(new ImageIcon(imgUrl).getImage());
/*
new ImageIcon(imgUrl).getImage()意思:
先new上一个ImageIcon,在根据指定的 URL 创建一个 ImageIcon
再由getImage()方法,返回此图标的 Image
最后由JFrame容器里的setIconImage(Image image)方法,设置要作为此窗口图标显示的图像
*/
setSize(600, 400);
setLocationRelativeTo(null); // 设置窗口居中显示
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
private void layoutCenter() {
// 布局userNameLabel
// 尽管 SpringLayout 简单易用,但它还要仿效其他多数布局管理器的行为。
// 对于某些功能(如 FlowLayout 提供的行断开),需要创建一个 Spring 类的专用子类。
Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel),
Spring.width(userTxt)), Spring.constant(20));
int offsetX = childWidth.getValue() / 2; // 设置用户名水平方向居中
springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX,
SpringLayout.HORIZONTAL_CENTER, centerPanel);
// 设置用户名垂直方向居中
springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);
/*
putConstraint(String e1, Component c1, int pad, String e2, Component c2)
将组件 c1 的边 e1 连接到组件 c2 的边 e2,边与边之间的距离固定。
*/
// userTxt
// userTxt的西边与userNameLabel的东边的距离为20
springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
// userTxt的北边与userNameLabel的北边的距离为0
springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);
// pwdLabel
// pwdLabel的东边与userNameLabel的东边的距离为0
springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
// pwdLabel的北边与userNameLabel的南边的距离为20
springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, userNameLabel);
// pwdField
// pwdField的西边与pwdLabel的东边的距离为20
springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
// pwdField的北边与pwdLabel的北边的距离为0
springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);
// loginBtn
// loginBtn的西边与pwdLabel的西边的距离为50
springLayout.putConstraint(SpringLayout.WEST, loginBtn, 50, SpringLayout.WEST, pwdLabel);
// loginBtn的北边与pwdLabel的南边的距离为20
springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 20, SpringLayout.SOUTH, pwdLabel);
// resetBtn
// resetBtn的西边与loginBtn的东边的距离为50
springLayout.putConstraint(SpringLayout.WEST, resetBtn, 50, SpringLayout.EAST, loginBtn);
// resetBtn的北边与loginBtn的北边的距离为0
springLayout.putConstraint(SpringLayout.NORTH, resetBtn, 0, SpringLayout.NORTH, loginBtn);
// register的西边与resetBtn的东边的距离为50
springLayout.putConstraint(SpringLayout.WEST, register, 100, SpringLayout.EAST, resetBtn);
// register的北边与resetBtn的北边的距离为0
springLayout.putConstraint(SpringLayout.NORTH, register, 100, SpringLayout.SOUTH, resetBtn);
}
public static void main(String[] args) {
new LoginView();
}
public JTextField getUserTxt() {
return userTxt;
}
public void setUserTxt(JTextField userTxt) {
this.userTxt = userTxt;
}
public JPasswordField getPwdField() {
return pwdField;
}
public void setPwdField(JPasswordField pwdField) {
this.pwdField = pwdField;
}
}
这种怎么运行啊
于 2024-06-17 22:06:31 首次发布