java界面问题
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextField extends JFrame
{
private JLabel lbl1 = new JLabel("姓名");
private JLabel lbl2 = new JLabel("密码");
private JButton btnLogin = new JButton("登录");
private JButton btnExit = new JButton("退出");
private JPasswordField field1 = new JPasswordField(10);
private JTextField field2 = new JTextField(10);
public TextField(String title)
{
super(title);
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(lbl1);
container.add(field2);
container.add(lbl2);
container.add(field1);
//注册时间监听器
HandEvent ev = new HandEvent();
field1.addActionListener(ev);
field2.addActionListener(ev);
container.add(btnLogin);
container.add(btnExit);
JDialog.setDefaultLookAndFeelDecorated(true);
btnLogin.addActionListener(ev);
btnExit.addActionListener(ev);
setSize(400,300);
setVisible(true);
}
public class HandEvent implements ActionListener
{
String output = " ";
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==field2)
JOptionPane.showMessageDialog(null,"你的姓名是:"+event.getActionCommand());
else if(event.getSource()==field1)
JOptionPane.showMessageDialog(null,"密码是:"+event.getActionCommand());
else if(field1.getText().toString().equals("") & field2.getText().toString().equals(""))
{
JOptionPane.showConfirmDialog(TextField.this,"用户名和密码不能为空!!!","警告",JOptionPane.YES_NO_OPTION,JOptionPane.ERROR_MESSAGE);
}
else if(field2.getText().toString().equals(""))
{
JOptionPane.showConfirmDialog(TextField.this,"用户名不能为空!!!","警告",JOptionPane.YES_NO_OPTION,JOptionPane.ERROR_MESSAGE);
}
else if(field1.getText().toString().equals(""))
{
JOptionPane.showConfirmDialog(TextField.this,"密码不能为空!!!","警告",JOptionPane.YES_NO_OPTION,JOptionPane.ERROR_MESSAGE);
}
else if(event.getSource() == btnExit)
{
System.exit(0);
}
}
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
TextField fi = new TextField("Login");
fi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
请问 一下大家我按退出按钮怎么不起做用啊!就是如果姓名和密码为空的话,还要弹出其他的对话框,而不是直接退出的!所以我觉得很奇怪!难道逻辑有什么问题吗!按道理说不论用户名和密码是否为空,只要已经点击了退出按钮,就应该退出啊!