三、簡單應(yīng)用題(共24分)
本題的功能是對(duì)下拉菜單項(xiàng)的操作,包括添加和刪除。頁面包括一個(gè)下拉菜單、一個(gè)文本框和兩個(gè)按鈕“刪除”和“添加”,選中下拉菜單的一項(xiàng)后,可以通過“刪除”按鈕從下拉菜單中刪除該項(xiàng),在文本框中填入字符串后,單擊“添加”按鈕就可以將該項(xiàng)添加到下拉菜單中,所有信息都將顯示在右側(cè)的文本域中。
import java.a(chǎn)wt.*;
import java.a(chǎn)wt.event.*;
public class java2 extends java.a(chǎn)pplet.Applet imple-
ments hemListener,ActionListener
{Choice choice;
TextField text;
TextArea area;
Button add,del;
public void init() .
{choice:new Choice();
text=new TextField(8);
area:new TextArea(6,15);
choice.a(chǎn)dd("音樂天地");
choice.a(chǎn)dd("武術(shù)天地");
choice.a(chǎn)dd("象棋樂園");
choice.a(chǎn)dd("交友聊天");
add=new Button("添加");
del=new Button("刪除");
add.a(chǎn)ddActionListener(this);
del.a(chǎn)ddActionListener(this);
choice.a(chǎn)ddItemListener(this);
add(choice);
add(del);add(text);add(add);add(area);
}
public void itemStateChanged(hemEvent e)
{String name= ;
int index=choice.getSelectedIndex();
area.setText("\n"+index+":"+name);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =add||e.getSource()= =
text)
{String name=text.getText();
if(name.length()>0)
{choice.a(chǎn)dd(name);
choice.select(name);
area.a(chǎn)ppend("\n添加"+name);
}
}
else if(e.getSource()= =del)
{choice.remove( );
area.a(chǎn)ppend("\n刪除"+choice.getSelectedItem
());
}
}
}

