?? 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 + -