求一个java应用程序编码,简单的带有图形用户界面的,有触发事件的。。。谢谢喽,急

 我来答
flyingFish211
2011-12-16 · TA获得超过2.1万个赞
知道大有可为答主
回答量:1.5万
采纳率:50%
帮助的人:1.1亿
展开全部
给你一个登陆验证的,用户名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();
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式