📄 example7_20.java
字号:
import java.awt.*;import java.awt.event.*;
class WindowChoice extends Frame implements
ItemListener,ActionListener
{ Choice choice;
TextField text;
TextArea area;
Button add,del;
WindowChoice()
{ setLayout(new FlowLayout());
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);
setSize(200,200);
setVisible(true);
validate();
}
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());
}
}
}
public class Example7_20
{
public static void main(String args[])
{
new WindowChoice();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -