import java.awt.*;
import javax.swing.*;
public class JComboBoxModelTest extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JComboBox<String> jc = new JComboBox<>(new MyComboBox());
JLabel jl = new JLabel("请选择证件:");
public JComboBoxModelTest() {
setSize(new Dimension(160, 180));
setVisible(true);
setTitle("在窗口中设置下拉列表框");
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(jl);
cp.add(jc);
}
public static void main(String[] args) {
new JComboBoxModelTest();
}
}
class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String> {
/**
*
*/
private static final long serialVersionUID = 1L;
String selecteditem = null;
String[] test = { "身份证", "军人证", "学生证", "工作证" };
public String getElementAt(int index) {
return test[index];
}
public int getSize() {
return test.length;
}
public void setSelectedItem(Object item) {
selecteditem = (String) item;
}
public Object getSelectedItem() {
return selecteditem;
}
public int getIndex() {
for (int i = 0; i < test.length; i++) {
if (test[i].equals(getSelectedItem()))
return i;
}
return 0;
}
}
写的那么懵懵哒,书里又不解释,视频也没说,看了1小时都不知道想表达些什么
JComboBox<String> jc = new JComboBox<>(new MyComboBox()); 里面的<String>和<>是什么意思?
JComboBox<String> jc = new JComboBox<>(new MyComboBox());为什么调用MyComboBox的构造方法就能搞定JComboBox里的内容了,下面的MyComboBox里面都没写构造方法
public void setSelectedItem(Object item) {
selecteditem = (String) item;
}
这里的selecteditem和item有是什么?是不是继承的那个类的某个变量,这段话有什么用?
没了,最后表示这课写的非常不满意!!!