📄 bookloginiframe.java
字号:
package com.lishan.iframe;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.text.Document;
import com.lishan.Library;
import com.lishan.dao.Dao;
import com.lishan.model.Operater;
import com.lishan.util.CreatedIcon;
import com.lishan.util.MyDocument;
public class BookLoginIFrame extends JFrame {
private JPasswordField password; // 登陆密码
private JTextField username; // 登陆用户
private JButton login; // 登陆按钮
private JButton reset; // 重新输入按钮
private static Operater user; // 用户
private class BookResetAction implements ActionListener {// 对重置事件监听
public void actionPerformed(final ActionEvent e) {
username.setText("");
password.setText("");
}
}
class BookLoginAction implements ActionListener {// 对登陆事件监听
public void actionPerformed(final ActionEvent e) {
//核对用户是不是管理员(只要管理员表中有该条记录,则代表此用户是管理员)
user = Dao.check(username.getText(), password.getText());
if (user.getName() != null) {//如果是
try {
//则显示主窗体,隐藏登陆窗体.
Library frame = new Library();
frame.setVisible(true);
BookLoginIFrame.this.setVisible(false);
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "只有管理员才可以登录");
username.setText("");
password.setText("");
}
}
}
/**
* 启动登陆界面
*/
public BookLoginIFrame() {// 构造登陆界面
super();
final BorderLayout borderLayout = new BorderLayout();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
borderLayout.setVgap(10); // 设置组件间的垂直间距
setTitle("图书管理系统登陆");
setBounds(100, 100, 285, 194);
final JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(new EmptyBorder(0, 0, 0, 0)); // 设置边框
getContentPane().add(panel);
final JPanel panel_2 = new JPanel();
final GridLayout gridLayout = new GridLayout(0, 2);
gridLayout.setHgap(5);
gridLayout.setVgap(5);
panel_2.setLayout(gridLayout);
panel.add(panel_2);
final JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.CENTER);// 设置标签内容沿 X 轴的对齐方式
label.setPreferredSize(new Dimension(0, 0));
label.setMinimumSize(new Dimension(0, 0));
panel_2.add(label);
label.setText("用 户 名");
username = new JTextField(20);
username.setPreferredSize(new Dimension(0, 0));
panel_2.add(username);
final JLabel label_1 = new JLabel();
label_1.setHorizontalAlignment(SwingConstants.CENTER);
panel_2.add(label_1);
label_1.setText("密 码");
password = new JPasswordField(20);
// 将编辑器与一个MyDocument文本文档关联。当前注册的工厂用来生成文档的一个视图,此视图经过重新验证之后由编辑器来显示。
// 将 PropertyChange 事件 ("document") 传播到每个侦听器。
password.setDocument(new MyDocument(6));
password.setEchoChar('*');
password.addKeyListener(new KeyAdapter() {
public void keyPressed(final KeyEvent e) {
if (e.getKeyCode() == 10)
login.doClick();
}
});
panel_2.add(password);
final JPanel panel_1 = new JPanel();
panel.add(panel_1, BorderLayout.SOUTH);
login = new JButton("登陆");
login.addActionListener(new BookLoginAction());
panel_1.add(login);
reset = new JButton("重置");
reset.addActionListener(new BookResetAction());
panel_1.add(reset);
final JLabel tupianLabel = new JLabel();
ImageIcon loginIcon = CreatedIcon.add("login.jpg");
tupianLabel.setIcon(loginIcon);
tupianLabel.setOpaque(true); // 使组件绘制其边界内的所有像素
tupianLabel.setBackground(Color.GREEN);
tupianLabel.setPreferredSize(new Dimension(260, 60));
panel.add(tupianLabel, BorderLayout.NORTH);
setVisible(true);
setResizable(false);
// this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static Operater getUser() {
return user;
}
public static void setUser(Operater user) {
BookLoginIFrame.user = user;
}
// public static void main(String[] args){
// new BookLoginIFrame();
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -