在个程序在信息查询时不能在JList上显示出信息,比如输入姓名(前面已经插入了的姓名)进行查询,面板上什么都没有,请问是为什么?怎么改?
import javax.swing.*;
import java.util.LinkedList;
import java.awt.*;
import java.awt.event.*;
//import java.util.*;
public class User1 implements ActionListener{
JFrame f;
JTextField tf1,tf2;
Checkbox cb1,cb2;
Choice c1,c2;
List ls;
JButton b1,b2;
Label lb1,lb2;
Panel p1,p2,p3,p4,p5;
LinkedList list=new LinkedList();
public void display(){
CheckboxGroup cg;
f=new JFrame("信息插入");
f.setSize(500,300);
f.setLocation(200,140);
f.setLayout(new GridLayout(1,2));
ls=new List();
f.add(ls);
p1=new Panel();
f.add(p1);
p1.setLayout(new GridLayout(6,1));
p2=new Panel();
p3=new Panel();
p1.add(p2);
p1.add(p3);
p4=new Panel();
p5=new Panel();
lb1=new Label("姓名");
lb2=new Label("学号");
tf1=new JTextField(16);
tf2=new JTextField(16);
p2.setLayout(new FlowLayout());
p2.add(lb1);
p2.add(tf1);
p3.add(lb2);
p3.add(tf2);
cg=new CheckboxGroup();
cb1=new Checkbox("男",cg,true);
cb2=new Checkbox("女",cg,false);
p1.add(p4);
p4.add(cb1);
p4.add(cb2);
c1=new Choice();
c1.addItem("湖北");
c1.addItem("江西");
//c1.addItemListener(this);
c2=new Choice();
c2.addItem("武汉");
c2.addItem("南昌");
//c2.addItemListener(this);
p1.add(c1);
p1.add(c2);
b1=new JButton("添加");
b2=new JButton("删除");
p1.add(p5);
p5.add(b1);
p5.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{
String str;
str=tf1.getText()+" "+tf2.getText();
if(cb1.getState())
str=str+" "+cb1.getLabel();
if(cb2.getState())
str=str+" "+cb2.getLabel();
str=str+" "+c1.getSelectedItem();
str=str+" "+c2.getSelectedItem();
ls.add(str);
list.add(tf1.getText());
list.add(tf2.getText());
if(cb1.getState())
list.add(cb1.getLabel());
if(cb2.getState())
list.add(cb1.getLabel());
list.add(c1.getSelectedItem());
list.add(c2.getSelectedItem());
//System.out.print(list);
(new haxun()).laop();
}
if(e.getSource()==b2)
{
ls.remove(ls.getSelectedIndex());
}
if(e.getSource()==b2)
{
ls.remove(ls.getSelectedIndex());
}
}
public static void main(String[] args) {
User1 us=new User1();
us.display();
}
}
class haxun extends JFrame implements ActionListener{
Label lb1,lb2;
JTextField tf1,tf2;
DefaultListModel list2=new DefaultListModel();
JList ta=new JList(list2);
JButton b;
Panel p1,p2,p3,p4;
public haxun(){
super("信息查找");
}
public void laop(){
Chaxun f=new Chaxun();
f.setSize(300,300);
f.setLocation(300,150);
f.setLayout(new GridLayout(2,1));
JTextArea ta=new JTextArea();
f.add(ta);
Panel p1=new Panel();
f.add(p1);
p1.setLayout(new GridLayout(3,1));
Panel p2=new Panel();
p1.add(p2);
Label lb1=new Label("姓名");
JTextField tf1=new JTextField(10);
p2.add(lb1);
p2.add(tf1);
Panel p3=new Panel();
p1.add(p3);
Label lb2=new Label("学号");
JTextField tf2=new JTextField(10);
p3.add(lb2);
p3.add(tf2);
Panel p4=new Panel();
p1.add(p4);
JButton b=new JButton("查找");
p4.add(b);
b.addActionListener(this);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b){
User1 u=new User1();
int j=u.list.indexOf(tf1.getText());
list2.addElement(u.list.get(j));
list2.addElement(u.list.get(j+1));
list2.addElement(u.list.get(j+2));
list2.addElement(u.list.get(j+3));
list2.addElement(u.list.get(j+4));
//ta.append(u.list.get(j));
//System.out.print(u.list);
}
}
}