当我运行此代码时,所选项目不可见。我已经尝试在没有运气的单独线程中运行它。
import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import java.awt.Dimension;
public class ScrollList extends JFrame {
int defaultValue;
ScrollList() {
Container cp = getContentPane();
JList list = createList();
defaultValue = 20;
cp.add(createScrollPane(list));
pack();
setVisible(true);
list.ensureIndexIsVisible(defaultValue);
}
JList createList() {
Integer[] model = new Integer[73];
JList list = new JList(model);
for (int i = 1; i < model.length; i++)
model[i] = i;
list.setSelectedIndex(defaultValue);
return list;
}
JScrollPane createScrollPane(JList list) {
JScrollPane s = new JScrollPane(createList());
s.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
s.setPreferredSize(new Dimension(100, 200));
return s;
}
public static void main(String[] args) {
new ScrollList();
}
}