📄 loginpanel.java
字号:
/*
* Created on 2005-4-13
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package ui;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import AccountsPack.User;
import AccountsPack.UserFactory;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class LoginPanel extends JPanel {
private JLabel jLabel = null;
private JTextField tfUserName = null;
private JLabel jLabel1 = null;
private JPasswordField pfPassword = null;
private JButton bnLogin = null;
private JButton bnCancel = null;
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getTfUserName() {
if (tfUserName == null) {
tfUserName = new JTextField();
tfUserName.setBounds(118, 25, 145, 22);
}
return tfUserName;
}
/**
* This method initializes jPasswordField
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getPfPassword() {
if (pfPassword == null) {
pfPassword = new JPasswordField();
pfPassword.setBounds(118, 74, 145, 22);
}
return pfPassword;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getBnLogin() {
if (bnLogin == null) {
bnLogin = new JButton();
bnLogin.setBounds(104, 125, 74, 22);
bnLogin.setText("Login");
bnLogin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
OnLogin();
}
});
}
return bnLogin;
}
public void OnLoginSuccess(String userName) {
UserFactory factory = new UserFactory();
User loginUser = factory.GetUser(-1);
loginUser.set_userName(userName);
loginUser.GetInfo();
new MainFrame();
MainFrame.getMainFrame().setCurrentUser(loginUser);
MainFrame.getMainFrame().init();
MainFrame.getMainFrame().setVisible(true);
JFrame parent = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this);
if (parent != null) {
parent.dispose();
}
}
public void OnCancel(){
JFrame parent = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this);
if (parent != null) {
parent.dispose();
}
}
public void OnLogin(){
String userName = getTfUserName().getText();
if (userName.equals("")) {
JOptionPane.showMessageDialog(this, "Please enter user name!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
String pwd = new String(getPfPassword().getPassword());
UserFactory factory = new UserFactory();
User loginUser = factory.GetUser(-1);
loginUser.set_userName(userName);
loginUser.set_password(pwd);
if (loginUser.Login()) {
OnLoginSuccess(userName);
}else {
JOptionPane.showMessageDialog(this, "Login failed.Check your user name and password!", "Error", JOptionPane.ERROR_MESSAGE);
}
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getBnCancel() {
if (bnCancel == null) {
bnCancel = new JButton();
bnCancel.setBounds(192, 125, 74, 22);
bnCancel.setText("Cancel");
bnCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
OnCancel();
System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return bnCancel;
}
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setTitle("Hotel online ordering system - Login");
mainFrame.setContentPane(new LoginPanel());
mainFrame.setSize(new Dimension(300, 200));
mainFrame.setResizable(false);
mainFrame.setLocation(300, 200);
mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mainFrame.setVisible(true);
}
/**
* This is the default constructor
*/
public LoginPanel() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
jLabel1 = new JLabel();
jLabel = new JLabel();
this.setLayout(null);
this.setSize(300, 186);
jLabel.setText("User name:");
jLabel.setBounds(22, 25, 76, 22);
jLabel1.setBounds(22, 74, 76, 22);
jLabel1.setText("Password:");
this.add(jLabel, null);
this.add(getTfUserName(), null);
this.add(jLabel1, null);
this.add(getPfPassword(), null);
this.add(getBnLogin(), null);
this.add(getBnCancel(), null);
}
} // @jve:decl-index=0:visual-constraint="81,32"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -