import?java.awt.Color;
import?java.awt.Font;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?java.util.Scanner;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JLabel;
import?javax.swing.JPasswordField;
import?javax.swing.JTextField;
import?javax.swing.border.TitledBorder;
/*編寫壹個應用程序,有壹個標題為“登錄”的窗口,能實現用戶名和密碼的輸入。
(1)如果用戶名和密碼輸入正確,則單擊“登錄”按鈕彈出“用戶登錄成功”的消息框;
(假設用戶名是admin,密碼是123456)。
(2)如果用戶名和密碼輸入錯誤,則單擊“登錄”按鈕彈出“用戶登錄失敗”的消息框。*/
public?class?Login?extends?JFrame{
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
Login?fReg=new?Login();
fReg.setVisible(true);//設置窗體可見
}
public?Login()?{
super();
setTitle("登錄");
setBounds(100,?160,?260,?210);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final?JLabel?lable=new?JLabel();
lable.setBorder(new?TitledBorder(null,?"",TitledBorder.DEFAULT_JUSTIFICATION,?TitledBorder.DEFAULT_POSITION,?null));
lable.setForeground(new?Color(255,?0,?0));
lable.setFont(new?Font("",?Font.BOLD,?18));
lable.setText("學生賬戶登錄");
lable.setBounds(60,?28,?120,?36);
getContentPane().add(lable);
final?JLabel?nameLable=new?JLabel();
nameLable.setText("姓名");
nameLable.setBounds(39,?83,?60,?15);
getContentPane().add(nameLable);
JTextField?nameField=new?JTextField();
nameField.setBounds(89,?80,?120,?21);
getContentPane().add(nameField);
final?JLabel?passwordLable=new?JLabel();
passwordLable.setText("密碼");
passwordLable.setBounds(39,?107,?60,?15);
getContentPane().add(passwordLable);
//JTextField?bornField=new?JTextField();
JPasswordField?passwordField=new?JPasswordField();
passwordField.setEchoChar('*');
passwordField.setBounds(89,?104,?120,?21);
getContentPane().add(passwordField);
final?JButton?exitButton=new?JButton();
exitButton.setText("退出");
exitButton.setBounds(141,?132,?68,?23);
getContentPane().add(exitButton);
exitButton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
JButton?button=(JButton)e.getSource();
String?buttonName=e.getActionCommand();
if(buttonName.equals("退出"))?{
System.exit(DO_NOTHING_ON_CLOSE);
}
}
});
final?JButton?loginButton=new?JButton();
loginButton.setText("登錄");
loginButton.setBounds(67,?132,?68,?23);
getContentPane().add(loginButton);
loginButton.addActionListener(new?ActionListener()?{
@Override
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
JButton?button=(JButton)e.getSource();
String?buttonName=e.getActionCommand();
//Scanner?scan=new?Scanner(System.in);
//String?nameField=scan.next();
//String?passwordField=scan.next();
String?userName=nameField.getText();
String?password=passwordField.getText();
if(buttonName.equals("登錄"))?{
if(userName.equals("admin")&&password.equals("123456"))?{
lable.setText("妳已經登錄了!");
}else?{
lable.setText("您登陸失敗!");
}
}
}
});
}
/*class?LoginCheck?{//編寫登錄驗證類
private?String?userName;//用戶名
private?String?password;//密碼
public?LoginCheck(String?userName,String?password)//復寫構造方法
{
this.userName=userName;//為用戶名賦值
this.password=password;//為密碼賦值
}
public?boolean?validate()//設置驗證方法
{
if("admin".equals(userName)&&"123456".equals(password))
{
return?true;//登錄成功
}else?{
return?false;//登錄失敗
}
}
}
*/
}
方法二:
import?javax.swing.JFrame;
import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.*;
public?class?One?extends?JFrame?implements?ActionListener{
private?JLabel?usernameLabel,passwordLabel,result;
private?JTextField?username,password;
private?JButton?login,exit;
public?One(){
init();
setVisible(true);
}
public?void?init(){
setTitle("登錄");
setSize(400,?500);
setLocation(400,?400);
setLayout(null);
usernameLabel=new?JLabel("用戶名");
username=new?JTextField();
//JPasswordField?password=new?JPasswordField();
//password.setEchoChar("*");
passwordLabel=new?JLabel("密碼");
password=new?JTextField();
result=new?JLabel();
login=new?JButton("登錄");
exit=new?JButton("退出");
usernameLabel.setBounds(20,?20,?30,?30);
username.setBounds(80,?20,?200,?40);
passwordLabel.setBounds(20,?80,?30,?30);
password.setBounds(80,?80,?200,?40);
login.setBounds(300,?20,?60,?40);
exit.setBounds(300,?80,?60,?40);
result.setBounds(100,?350,?120,?20);
add(usernameLabel);
add(passwordLabel);
add(username);
add(password);
add(login);
add(exit);
add(result);
login.addActionListener(?this);
exit.addActionListener(?this);
}
public?void?actionPerformed(ActionEvent?e)?{
//?TODO?Auto-generated?method?stub
if(e.getSource()==login){
String?user=username.getText();
String?paw=password.getText();
if(user.equals("admin")||paw.equals(12345)){
JOptionPane.showMessageDialog(getComponent(0),?"登陸成功");
dispose();
}
else{
JOptionPane.showMessageDialog(getComponent(0),?"登陸失敗");
}
}
}
public?static?void?main(String[]?args)?{
//?TODO?Auto-generated?method?stub
new?One();
}
}
希望對妳有幫助!