填空题 本题的功能是对列表项的操作,包括删除、添加和反选。窗口中有两个列表框和5个按钮,按钮标签代表着移除列表项的方向,“>”代表只移除选中的列表项,“>>”代表移除所有的列表项,“!”代表反向选择列表项。
import java. awt. * ;
import java. awt. event. * ;
class java3 extends Frame implements ActionListener&ItemListener
final static int ITEMS=10;
List ltList=new List(ITEMS, true);
List rtList=new List(0,true);
java3()
super("java3");
addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent e)
System. exit(0);

);
GridBagLayout gbl=new GridBagLayout();
setLayout (gbl);
add(ltList, 0,0,1,5,1.0,1.0);
add(rtList, 2,0,1,5,1.0,1.0);
ltList, addAetionListener(this);
ltList, addItemListener(this);
rtList, addAetionListener(this);
rtList, addhemListener(this);
Button b;
add(b=new Button(">"), 1,0,1,1,0,1.0);
b. addAetionListener(this);
add(b=new Button(">>"), 1,1,1,1,0,1.0);
b. addAetionListener(this);
add(b=new Button("<"), 1,2,1,1,0,1.0);
b. addActionListener(this);
add(b=new Button("<<"), 1,3,1,1,0,1.0);
b. addActionListener(this);
add(b=new Button("!"), 1,4,1,1,0,1.0);
b. addAetionListener(this);
for (int i=0; i<ITEMS; i++)
ltList. add("item"+i);

pack();
show();

void add(Component comp,
int x, int y, int w, int h, double weightx, double weighty)
GridBagLayout gbl=(GridBagLayout)getLayout();
GridBagConstraints c=new GridBagConstraints();
c. fill=GridBagConstraints. BOTH;
c. gridx=x;
c. gridy=y;
c. gridwidth=w;
c. gridheight=h;
c. weightx=weightx;
c. weighty=weighty;
add(comp);
gbl. setConstraints(comp, c);

void reverseSelections(List 1)
for(int i=0; i<1. length(); i++)
if(1. islndexSelected(i))
1. deselect(i);
else
1. select(i);



void deselectAll(List 1)
for (int i=0; i<1. getItemCount(); i++)
1. deseleet(i);


void replaceItem(List 1, String item)
for (int i=0; i<1. getItemCount(); i++)
if(1. getItem(i), equals(item))
1. replaceItem(item+"*", i);



void move(List l1, List l2, boolean all)
if (all)
for (int i=0; i<l1. getItemCount(); i++)
l2. add(l1. getItem(i));

l1. removeAll();
else
String[] items=l1. getSelectedItems();
int[] itemIndexes=l1. getSelectedIndexes();
deselectAll(l2);
for (int i=0; i<items. length; i++)
l2. add(items[i]);
l2. select(l2. gethemCount()-1);
if(i=0)
12. makeVisible(l2, getltemCount()-1);


for (int i=itemlndexes. length-1; i>=0; i--)
l1. remove(itemIndexes[i]);



public void actionPerformed(ActionEvent evt)
String arg=evt. getActionCommand();
if (">". equals(arg))
move(ltList, rtList, false);
else if(">>". equals(arg))
move(ltList, rtList, true);
else if("<". equals(arg))
move(rtList, ltList, false);
else if("<<". equals(arg))
move(rtList, ltList, true);
else if("!". equals(arg))
if (ltList. getSelectedhems(), length>0)
reverseSeleetions(ltList);
else if (rtList. getSelectedltems(). length>0)
reverseSelections(rtList);

else
Object target=evt. getSource();
if (target=rtList||target==ltList)
replaceItem((List) target, arg);



public void itemStatedChanged(ItemEvent ent)
List target=(List)evt. getSource();
if (target==ltList)
deselectAll(rtList);
else if(target==rtList)
deselectAll(ltList)


public static void main(String[] args)
new java3()



  • 1、
【正确答案】 1、{{*HTML*}}第1处:extends Frame implements ActionListener, ItemListener
第2处:for(int i=0; i<l. getItemCount(); i++)
第3处:public void ItemStateChanged(ItemEvent evt)    
【答案解析】[解析] 第1处类可以实现多个接口,接口之间用“,”隔开;第2处reverseSelections方法实现的是反选,遍历列表获得列表元素数应使用的是getItemCount()方法;第3处Java是大小写敏感的。