用JAVA做一个登陆界面,输入三次密码,错就退出。要求程序简单易懂。太复杂的看不懂!

我刚刚学JAVA,还未学到输入输出流.... 我刚刚学JAVA,还未学到输入输出流. 展开
 我来答
flyingFish211
2011-11-30 · TA获得超过2.1万个赞
知道大有可为答主
回答量:1.5万
采纳率:50%
帮助的人:1.1亿
展开全部
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Logon {

private JFrame f = new JFrame("Logon");
private JTextField username = new JTextField(10);
private JPasswordField password = new JPasswordField(10);
private JLabel user = new JLabel("User");
private JLabel pwd = new JLabel("Password");
private JButton logon = new JButton("Logon on");
private int count = 0;

public Logon(){
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 2));

p.add(user);
p.add(username);
p.add(pwd);
p.add(password);

f.add(p, BorderLayout.NORTH);
f.add(logon, BorderLayout.SOUTH);

logon.addMouseListener(new MouseListener(){

public void mouseClicked(MouseEvent e) {
if(count < 3){
if(username.getText().trim().equals("") || password.getText().trim().equals("")){
JOptionPane.showMessageDialog(null, "Password/Username is blank. Please input!");
return;
}
//假定用户为admin,密码为abc123
if(username.getText().equals("admin") && password.getText().equals("abc123")){
JOptionPane.showMessageDialog(null, "Logon on success");
}else{
username.setText("");
password.setText("");
JOptionPane.showMessageDialog(null, "Username/password incorrect. Please try again");
count++;
}

}else{
JOptionPane.showMessageDialog(null, "You have tried 3 times. Program exit!");
System.exit(0);
}

}

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setBounds(200, 200, 400, 400);
f.pack();

}

public static void main(String[] args) {
new Logon();
}

}
lonelone11
2011-11-30
知道答主
回答量:3
采纳率:0%
帮助的人:3.2万
展开全部
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Login extends JFrame {

private JPanel contentPane;
private JTextField username;
private JPasswordField password;
private int failCount = 0;
private JLabel tipLabel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Login() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 300, 200);
contentPane = new JPanel();
contentPane.setLayout(null);
setContentPane(contentPane);

JLabel label = new JLabel("用户名");
label.setBounds(46, 51, 54, 15);
contentPane.add(label);

JLabel label_1 = new JLabel("密码");
label_1.setBounds(46, 84, 54, 15);
contentPane.add(label_1);

username = new JTextField();
username.setBounds(106, 48, 88, 21);
contentPane.add(username);
username.setColumns(10);

password = new JPasswordField();
password.setBounds(106, 81, 88, 21);
contentPane.add(password);

JButton submit = new JButton();
submit.setBounds(71, 109, 93, 23);
submit.setAction(new AbstractAction(){
@Override public void actionPerformed(ActionEvent e) {
boolean result = false; //TODO 验证登陆
if(result){
//TODO 登陆成功.
}else{
failCount++;
tipLabel.setText("用户名或密码错误!");
if(failCount >=3){
JOptionPane.showMessageDialog(Login.this, "登陆失败三次,退出!");
System.exit(0);
}
}
}
});
submit.setText("登陆");
contentPane.add(submit);

tipLabel = new JLabel("");
tipLabel.setForeground(Color.red);
tipLabel.setBounds(10, 10, 264, 15);
contentPane.add(tipLabel);
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zhanghengxye
2011-11-30 · TA获得超过326个赞
知道小有建树答主
回答量:1172
采纳率:100%
帮助的人:462万
展开全部
登陆你肯定没问题啊,关键是如何判断是第几次,可以做个session,设置好参数=3,然后递减
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
iceworldvip
2011-11-30
知道答主
回答量:25
采纳率:0%
帮助的人:15.3万
展开全部
作业题……
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式