/**
* 列表
* @time 4:53:26 PM
* @author retacn yue
* @Email
[email protected]
*/
public class Test_ListComboExample extends JPanel {
private static JFrame frame;
private static final long serialVersionUID = 1L;
/**
* 构造器
*/
private Test_ListComboExample() {
setLayout(new GridLayout(2, 2));
JList list = new JList(new ListModelExample());
list.setVisibleRowCount(4);
JScrollPane pane = new JScrollPane();
pane.setViewportView(list);
add(pane);
JComboBox comboBox = new JComboBox(new ComboModelExample());
add(comboBox);
}
public static void main(String[] args) {
frame = new JFrame();
Test_ListComboExample comboExample = new Test_ListComboExample();
frame.getContentPane().add("Center", comboExample);
frame.setSize(200, 200);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
/**
* 列表框
*/
class ListModelExample extends AbstractListModel {
private static final long serialVersionUID = 1L;
String values[] = { "张三", "李四", "王五", "赵六", "猪八", "猴九" };
public Object getElementAt(int index) {
return values[index];
}
public int getSize() {
return values.length;
}
}
/**
* 组合框
*/
class ComboModelExample extends ListModelExample implements ComboBoxModel {
private static final long serialVersionUID = 1L;
Object item;
public Object getSelectedItem() {
return item;
}
public void setSelectedItem(Object anItem) {
item = anItem;
}
}