1、下拉列表框组件JComboBox
将选项隐藏起来,当用户使用,点击▽选择选项
必须实现方法:
public void setSelectedItem(Object item); public Object getSelectedItem();
三种操作列表的方法:
getSize(); getItemAt(int index); addItem(Object obj);
example:
//JComboBoxModelTest.java package JComboBox; import javax.swing.JFrame; import javax.swing.JComboBox; import javax.swing.JLabel; import java.awt.Dimension; import javax.swing.WindowConstants; import java.awt.Container; import java.awt.FlowLayout; public class JComboBoxModelTest extends JFrame{ JComboBox jc = new JComboBox(new MyComboBox()); JLabel jl = new JLabel("请选择证件"); public JComboBoxModelTest(){ setSize(new Dimension(160,80)); setVisible(true); setTitle("在窗口设置下拉列表框"); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); Container container = getContentPane(); container.setLayout(new FlowLayout()); container.add(jl); container.add(jc); } public static void main(String[] args){ new JComboBoxModelTest(); } }
//MyComboBox.java /*自定义一个实现ComboBoxModel接口继承AbstractListModel类的模型类MyComboBox*/ package JComboBox; import javax.swing.AbstractListModel; import javax.swing.ComboBoxModel; import javax.swing.JFrame; public class MyComboBox extends AbstractListModel implements ComboBoxModel { String selecteditem = null; String []test = {"身份证","军人证","学生证","工作证"}; public int getSize() { // TODO Auto-generated method stub return test.length; } //返回指定索引处的值 public Object getElementAt(int index) { // TODO Auto-generated method stub return test[index]; } public void setSelectedItem(Object item) { // TODO Auto-generated method stub selecteditem = (String) item; } public Object getSelectedItem() { // TODO Auto-generated method stub return selecteditem; } public int getIndex(){ for(int i = 0;i < test.length;i++){ if(test[i].equals(getSelectedItem())); return i; } return 0; } }
2、列表组件JList
初始化列表
String[] contents = {"list1","list2","list3","list4"}; JList jl = new JList(contents); //使用Vector向量对象 Vector contents = new Vector(); contents.add("list1"); contents.add("list2"); contents.add("list3"); contents.add("list4"); JList jl = new JList(contents);
example
//MyListModel类继承AbstractListModel抽象类实现数据模型 package JList; import javax.swing.AbstractListModel; public class MyListModel extends AbstractListModel{ private String[] contents = {"list1","list2","list3","list4","list5","list6"}; public int getSize() { // TODO Auto-generated method stub return contents.length; } public Object getElementAt(int index) { // TODO Auto-generated method stub if(index < contents.length) return contents[index++]; else return null; } }
package JList; import javax.swing.JFrame; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.WindowConstants; public class JListTest extends JFrame{ public JListTest(){ Container container = getContentPane();//获取窗体容器 container.setLayout(new BorderLayout());//设置窗体布局 JList jl= new JList(new MyListModel());//使用自定义模型创建JList JScrollPane js = new JScrollPane(jl);//创建滚动面板控制JList container.add(js); setTitle("在这个窗体使用了列表框"); setSize(119,150); setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args){ new JListTest(); } }