public class Swimmer{
private String name;
private int age;
private String club;
private boolean female;
public Swimmer(String name,int age,String club,boolean female){
this.name=name;
this.age=age;
this.club=club;
this.female=female;
}
public boolean isFemale(){
return female;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public String getClub(){
return club;
}
}
//get a vector of swimmers who are (female)
public Vector getList(boolean female){
Vector v=new Vector();
for(int i=0;i Swimmer swm=(Swimmer)kids.elementAt(i);
if(swm.isFemal()==female) v.add(swm);
}
return v;
}
public void actionPerformed(ActionEvent e){
//listen for button clicks and do the right thing
Object obj=e.getSource();
if(obj==female) loadFemales();
if(obj==male) loadMales();
if(obj==both) loadBoth();
}
private void loadFemales(){
//display female swimmers
Vector v=swimmers.getList(true);
loadList(v);
}
private void loadMales(){
//display male swimmers
Vector v=swimmers.getList(false);
loadList(v);
}
//the Command interface
public interface Command{
public void execute();
}
//abstract radio button class
public abstract class SexButton
extends JRadioButton implements Command{
protected Swimmers simmers;
//JawList is a subclass of JScrollPane contained a JList;
protected JawList kidList;
public SexButton(String title,Swimmers sw,
JawList klist,ActionListener al){
super(title);
swimmers=sw;
kidList=klist;
addActionListener(al);
}
//abstract execute method
public abstract void execute();
}
//radio button to select female swimmers
public class FemaleButton extends SexButton{
public execute(){
Vector v=swimmers.getList(true);
loadList(v);
}
private void loadList(Vector v){
kidList.clear();
for(int i=0;i Swimmer swm=(Swimmer)v.elementAt(i);
kidList.add(swm.getName());
}
}
}
public void actionPerformed(ActionEvent e){
Command cmd=(Command)e.getSource();
cmd.execute();
}
新闻热点
疑难解答