为什么提示框会出现两次?

yoshubom yoshubom 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
//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){

}

}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
yoshubom
Java SE 62,620 成员 307,251 内容