2016年計(jì)算機(jī)二級《JAVA》模擬簡單應(yīng)用試題

字號:

三、簡單應(yīng)用題
    本題的功能是對下拉菜單項(xiàng)的操作,包括添加和刪除。頁面包括一個下拉菜單、一個文本框和兩個按鈕“刪除”和“添加”,選中下拉菜單的一項(xiàng)后,可以通過“刪除”按鈕從下拉菜單中刪除該項(xiàng),在文本框中填入字符串后,單擊“添加”按鈕就可以將該項(xiàng)添加到下拉菜單中,所有信息都將顯示在右側(cè)的文本域中。
    import java.awt.*;
    import java.awt.event.*;
    public class java2 extends java.applet.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.add("音樂天地");
    choice.add("武術(shù)天地");
    choice.add("象棋樂園");
    choice.add("交友聊天");
    add=new Button("添加");
    del=new Button("刪除");
    add.addActionListener(this);
    del.addActionListener(this);
    choice.addItemListener(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.add(name);
    choice.select(name);
    area.append("\n添加"+name);
    }
    }
    else if(e.getSource()= =del)
    {choice.remove( );
    area.append("\n刪除"+choice.getSelectedItem
    ());
    }
    }
    }