组合框(下拉列表)(Combo box(drop-down list))
2013-08-31 18:21
267 查看
与一组单选按钮的功能类似,下拉列表也用来强制用户从一组可能的元素中只选择一个。
不过,这种方法更加紧凑,而且在不会使用户感到迷惑的前提下,改变下拉列表中的内容
更容易(你也可以动态改变单选按钮,不过这显然不合适)。
缺省状态下,JComBox组件与Windows操作系统下的组合框并不完全相同,后者允许你
从列表中选择或者自己输入。要想得到这样的行为,你必须调用setEditable( )方法。使用
JComboBox,你能且只能从列表中选择一个元素。在下面的例子里,JComboBox开始已
经具有一些元素,然后当一个按钮按下的时候,将向组合框中加入新的元素。
//: c14:ComboBoxes.java
// Using drop-down lists.
// <applet code=ComboBoxes width=200 height=125></applet>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.bruceeckel.swing.*;
public class ComboBoxes extends JApplet {
private String[] description = {
"Ebullient", "Obtuse",
"Recalcitrant", "Brilliant",
"Somnescent", "Timorous",
"Florid", "Putrescent"
};
private JTextField t = new JTextField(15);
private JComboBox c = new JComboBox();
private JButton b = new JButton("Add items");
private int count = 0;
public void init() {
for(int i = 0; i < 4; i++)
c.addItem(description[count++]);
t.setEditable(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(count < description.length)
c.addItem(description[count++]);
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.setText("index: "+ c.getSelectedIndex() + " " +
((JComboBox)e.getSource()).getSelectedItem());
}
});
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(t);
cp.add(c);
cp.add(b);
}
public static
void main(String[] args) {
Console.run(new ComboBoxes(), 200, 125);
}
} ///:~
上例中的JtextField被用来显示“被选中的索引”(当前被选中元素的序号)和组合框中被
选中元素的文本。
不过,这种方法更加紧凑,而且在不会使用户感到迷惑的前提下,改变下拉列表中的内容
更容易(你也可以动态改变单选按钮,不过这显然不合适)。
缺省状态下,JComBox组件与Windows操作系统下的组合框并不完全相同,后者允许你
从列表中选择或者自己输入。要想得到这样的行为,你必须调用setEditable( )方法。使用
JComboBox,你能且只能从列表中选择一个元素。在下面的例子里,JComboBox开始已
经具有一些元素,然后当一个按钮按下的时候,将向组合框中加入新的元素。
//: c14:ComboBoxes.java
// Using drop-down lists.
// <applet code=ComboBoxes width=200 height=125></applet>
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import com.bruceeckel.swing.*;
public class ComboBoxes extends JApplet {
private String[] description = {
"Ebullient", "Obtuse",
"Recalcitrant", "Brilliant",
"Somnescent", "Timorous",
"Florid", "Putrescent"
};
private JTextField t = new JTextField(15);
private JComboBox c = new JComboBox();
private JButton b = new JButton("Add items");
private int count = 0;
public void init() {
for(int i = 0; i < 4; i++)
c.addItem(description[count++]);
t.setEditable(false);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(count < description.length)
c.addItem(description[count++]);
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.setText("index: "+ c.getSelectedIndex() + " " +
((JComboBox)e.getSource()).getSelectedItem());
}
});
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(t);
cp.add(c);
cp.add(b);
}
public static
void main(String[] args) {
Console.run(new ComboBoxes(), 200, 125);
}
} ///:~
上例中的JtextField被用来显示“被选中的索引”(当前被选中元素的序号)和组合框中被
选中元素的文本。
相关文章推荐
- 多选下拉框 multiple select drop down list
- AxureRP7.0教程 部件详解 Droplist 下拉列表
- AxureRP7.0基础教程系列 部件详解 Droplist 下拉列表
- Axure RP Pro - 相关问题 - 原型中Droplist“下拉列表”部件在IE6中总显示在最前面
- jquery插件——下拉列表插件(jQuery Simple Drop-Down Menu Plugin)
- Axure RP Pro - 相关问题 - 在规格文档中动态面板中的droplist下拉列表框部件会重复输出选项列表
- 利用InfoPath实现SharePoint Server 2013列表的级联选择(Cascading Drop Down List)
- Excel - 使用跨sheet定义Drop_List 下拉列表
- 64、通过PickListValuesUtil,Schema查看下拉列表中的值
- spinner (drop down list)
- 在CComboBox组合框控件中 使用AddString()函数修改下拉列表项
- ASP.NET.MVC--注解和反射将Enum转换List<SelectListItem>为@Html.DropDownListFor()下拉列表的通用方法
- UNITY 3D 下拉列表的设置(popup list)
- Remedy Developer Stuido Drop-Down List 和 Character的default value的区别
- react native 中下拉列表FlatList组件的讲解以及实例demo
- 请教:JSP里如何实现下拉组合框,既能从列表中选择也能自由输入,同时输入时自动定位到与已输字符最相符的列表项?
- 在js中如何遍历List集合,并且动态渲染下拉列表数据
- MFC List Control 控件添加单元格编辑和单元格下拉列表项
- 组合框-----选中下拉列表中数据时所产生的事件
- java界面编程(8) ------ 组合框(下拉列表)