求一个java应用程序编码,简单的带有图形用户界面的,有触发事件的。。。谢谢喽,急
展开全部
给你一个登陆验证的,用户名admin,密码abc123,如果3次都没有输入正确,程序退出
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;
}
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.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;
}
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询