📄 example4_16.java
字号:
/* 演示下拉列表及列表 */
import java.awt.*;
import java.awt.event.*;
public class Example4_16 extends Frame implements
ItemListener,ActionListener
{
Choice choice;
TextField text;
List list;
Button add,del;
private class WindowCloser extends WindowAdapter
{ public void windowClosing(WindowEvent we)
{System.exit(0);}
}
Example4_16()
{
super("演示下拉列表及列表");
setSize(200,200);
setVisible(true);
setLayout(new FlowLayout());
choice=new Choice();
text=new TextField(8);
list=new List(6,true);
choice.add("产品展示");
choice.add("人才天地");
choice.add("企业名录");
choice.add("行业资讯");
add=new Button("添加");
del=new Button("删除");
add(choice); add(del);
add(text); add(add);
add(list);
validate();
add.addActionListener(this);
del.addActionListener(this);
choice.addItemListener(this);
addWindowListener(new WindowCloser());
}
public void itemStateChanged(ItemEvent e)
{ String name=choice.getSelectedItem();
int index=choice.getSelectedIndex();
list.add("\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);
list.add("\n添加"+name);
}
}
else if(e.getSource()==del)
{ choice.remove(choice.getSelectedIndex());
list.add("\n删除"+choice.getSelectedItem());
}
}
public static void main(String[] args)
{ new Example4_16(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -