疑问为什么下面的程序当文本输入张三的时候提交按钮,显示1,2号标签,然后我输入李四还是显示1,2号标签,而不是显示2,3号标签呢,正常李四显示的标签是2,3号标签
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class s extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JLabel label;
private JLabel label_1;
private JLabel label_2;
private JLabel lblNewLabel;
private ImageIcon background;
JLabel labellogo;
private Label label_4;
private Label label_5;
private Label label_3;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
s frame = new s();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public s() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
textField = new JTextField();
contentPane.add(textField, BorderLayout.NORTH);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(67, 102, 162, 32);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("请输入帐号");
//事件处理
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String str=textField_1.getText().trim();
if(str.equals("")){
JOptionPane.showMessageDialog(contentPane, "请输入帐号名字!");
}
else if(str.contains(" ")){
JOptionPane.showMessageDialog(contentPane,
"搜索关键字中请不要包含空格!");
}
else{
String rs="";
//如果输入张三显示1,2号标签,如果输入李四显示2,3号标签,其他帐号显示1,2,3号标签
if(str.equals("张三")){
rs="1,2";
}
else if(str.equals("李四")){
rs="2,3";
}
else{
rs="1,2,3";
}
String rss[]=rs.split(",");
int tx=130;
//这里会涉及到100个label标签,目前测试阶段只给与3个数字
label_3 = new Label("1"); //1号标签
label_4 = new Label("2"); //2号标签
label_5 = new Label("3");//3号标签
contentPane.add(label_3);
contentPane.add(label_4);
contentPane.add(label_5);
contentPane.updateUI();
label_3.setVisible(false);
label_4.setVisible(false);
label_5.setVisible(false);
Map map = new HashMap();
map.put("1", label_3);
map.put("2", label_4);
map.put("3", label_5);
// Set set =map.keySet();
// Iterator it=set.iterator();
// while(it.hasNext()){
// String s= (String) it.next();
// Label l=(Label)map.get(s);
//
// contentPane.add(l);