为什么提示框会出现两次?
//ListDemo.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ListDemo extends JFrame implements FocusListener {
TextField txtInput1 = new TextField();
TextField txtInput2 = new TextField();
List lst = new List();
JLabel lbl = new JLabel("Sperator");
JButton btn = new JButton("Add");
JPanel plst = new JPanel();
static Boolean show = false;
public ListDemo(){
Container con = getContentPane();
setLayout(new BorderLayout());
con.add("North",plst);
con.add("Center",lst);
con.add("South",btn);
plst.setLayout(new GridLayout(3,1));
plst.add(txtInput1);
plst.add(txtInput2);
plst.add(lbl);
txtInput1.addFocusListener(this);
txtInput2.addFocusListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String [] args){
new ListDemo();
}
public void focusLost(FocusEvent fe){
if (((TextField)fe.getSource()).getText().trim().equals("")){
System.out.println(show);
System.out.println(((TextField)fe.getSource()).getName());
show =!show;
if (show){
((TextField)fe.getSource()).requestFocus();
JOptionPane.showMessageDialog(null,"请输入内容","ERROR",JOptionPane.ERROR_MESSAGE);
}
//show =!show;
}
}
public void focusGained(FocusEvent fe){
}
}