1.上一篇完成了 借书等核心任务
这一篇主要是登陆,租用界面的编制了
继承,接口,重写,匿名内部类,就地实例化,按钮,文本框,密码框,密码验证,回车转换焦点,回车自动点击登陆, 退出验证,关闭主界面返回登陆窗口 恩…… 差不多了……
package com.lovo.ui;然后登陆成功转到主界面,呵呵意思一下。主界面
import java.awt.Component;
import java.awt.Event;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.security.auth.kerberos.KeyTab;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
* 类:登陆界面设置
* @author Abe
* 父类JDialog 接口ActionListenner
*/
@SuppressWarnings("serial")
public class LoginFrame extends JDialog implements ActionListener {
private JButton loginButton, cancelButton;
private JLabel uidLabel, pwdLabel;
private JTextField uidField;
private JPasswordField pwdField;
public LoginFrame() {
this.setSize(320, 240);
this.setTitle("学院图书馆管理系统");
this.setResizable(false);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setLayout(null);
/**
* 匿名内部类:windows面板X按钮监听设置
*/
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(null, "亲~真的要离开我吗?~") == 0) {
System.exit(0);
}
}
});
initComponents(); //初始化
}
/**
* 方法:组件添加
*/
private void initComponents() {
loginButton = new JButton("登陆");
loginButton.setBounds(70, 160, 80, 30);
this.add(loginButton);
cancelButton = new JButton("取消");
cancelButton.setBounds(170, 160, 80, 30);
this.add(cancelButton);
uidLabel = new JLabel("用户名:");
uidLabel.setBounds(50, 50, 60, 30);
this.add(uidLabel);
pwdLabel = new JLabel("密 码:");
pwdLabel.setBounds(50, 100, 60, 30);
this.add(pwdLabel);
uidField = new JTextField();
uidField.setBounds(120, 50, 120, 30);
this.add(uidField);
pwdField = new JPasswordField();
pwdField.setBounds(120, 100, 120, 30);
this.add(pwdField);
Font font = new Font("微软雅黑", Font.PLAIN, 14);
for (Component c : this.getContentPane().getComponents()) {
c.setFont(font);
if (c instanceof JButton) {
((JButton) c).addActionListener(this);
}
}
/**
* 匿名内部类:键盘监听器 回车换行 回车点enter
* 后来发现 可以用监听器监听uidField和pwdField
* 达到同样的效果……懒得改了……
*/
uidField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 10){
pwdField.requestFocus();
}
}
});
pwdField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
if (e.getKeyCode() == 10) {
loginButton.doClick();
}
}
});
}
/**
* main方法
* @param args
*/
public static void main(String[] args) {
new LoginFrame().setVisible(true);
}
/**
* 重写方法:监听事件处理
*/
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = uidField.getText();
String password = new String(pwdField.getPassword());
if (username.equals("admin") && password.equals("123456")) {
this.setVisible(false);
new MainFrame(this).setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!", "错误",
JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == cancelButton) {
System.exit(0);
}
}
}
package com.lovo.ui;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* 类:主界面
* @author Abe
*/
@SuppressWarnings("serial")
public class MainFrame extends JFrame {
private LoginFrame login;
public MainFrame(LoginFrame login){
this.login = login;
this.setTitle("图书租赁管理系统~");
this.setSize(800, 600);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (JOptionPane.showConfirmDialog(null, "亲~真的要离开我吗?~") == 0) {
MainFrame.this.dispose();
MainFrame.this.login.setVisible(true);
}
}
});
}
}