package lab;
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();
}
}
希望对你有帮助!