当前位置: 代码迷 >> J2SE >> swing中按钮退出有关问题
  详细解决方案

swing中按钮退出有关问题

热度:216   发布时间:2016-04-24 01:37:57.0
swing中按钮退出问题
public class Login extends javax.swing.JFrame implements ActionListener{
 
private static final long serialVersionUID = 1L;
private JLabel jLabel1;  
private JLabel jLabel2;  
private JPasswordField txtpassword;  
private JButton btnCanel;  
private JButton btnOK;  
private JTextField txtuser;  
 
public Login() {  
super();  
initGUI();  
}  
//初始化界面  
private void initGUI() {  
try {  
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);  
getContentPane().setLayout(null);  
{  
jLabel1 = new JLabel();  
getContentPane().add(jLabel1);  
jLabel1.setText("\u7528\u6237\u540d:");  
jLabel1.setBounds(79, 106, 42, 15);  
}  
{  
jLabel2 = new JLabel();  
getContentPane().add(jLabel2);  
jLabel2.setText("\u5bc6 \u7801:");  
jLabel2.setBounds(79, 140, 42, 15);  
}  
{  
txtuser = new JTextField();  
getContentPane().add(txtuser);  
txtuser.setBounds(144, 103, 153, 22);  
}  
{  
txtpassword = new JPasswordField();  
getContentPane().add(txtpassword);  
txtpassword.setBounds(144, 137, 153, 22);  
}  
{  
btnOK = new JButton();  
getContentPane().add(btnOK);  
btnOK.setText("\u8fdb\u5165");  
btnOK.setBounds(103, 178, 60, 22);  
btnOK.addActionListener(this);  
 
}  
 
{  
btnCanel = new JButton();  
getContentPane().add(btnCanel);  
btnCanel.setText("\u9000\u51fa");  
btnCanel.setBounds(202, 178, 60, 22);  
}  
pack();  
this.setSize(400, 300);  
} catch (Exception e) {  
e.printStackTrace();  
}  
}  
   
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Login inst = new Login();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

public void actionPerformed(ActionEvent e) {


if(e.getSource().equals(btnOK)){
// System.out.println("d2");

User user = new User();
user.setUsername(txtuser.getText());
char ps[] = txtpassword.getPassword();
String psd = new String(ps);
user.setPassword(psd);

UserDAO userdao = new UserDAO();
String flag = userdao.queryAllEmp(user);
if(flag.equals("YES")) {
System.out.println("登录成功");
}else{
JOptionPane.showMessageDialog(this, "用户名或密码不正确!!!");
txtpassword.setText("");
}
}else if(e.getSource().equals(btnCanel)) {
System.exit(0);
}
}
}
请教下用鼠标触发退出程序事件,错在哪里?还是别的错了

------解决方案--------------------
探讨