example13_4.java

来自「不错的教程 适合中高级人员的使用」· Java 代码 · 共 43 行

JAVA
43
字号
import java.awt.*;import java.awt.event.*;
public class Example13_4 extends java.applet.Applet implements
ItemListener,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("武术天地");
      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(ItemEvent e)
   {  String name=choice.getSelectedItem();
      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(choice.getSelectedIndex());
           area.append("\n删除"+choice.getSelectedItem());
        }
   }
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?