4个回答
展开全部
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();
}
}
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();
}
}
展开全部
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);
}
}
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
登陆你肯定没问题啊,关键是如何判断是第几次,可以做个session,设置好参数=3,然后递减
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询