我正在寻找一个Swing
组件,它结合了JList
的外观和JSpinner
提供的同时响应滚动功能的上下功能。有谁知道这样的事情,如果没有,我如何制作自己的Swing
组件?我需要它来进行我想要制作的游戏...
答案 0 :(得分:0)
你可以这样解决:
JList<String> list = new JList<String>(new String[]{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"});
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayout(new BorderLayout());
list.setSelectedIndex(0);
JScrollPane scrollpane = new JScrollPane(list);
scrollpane.setPreferredSize(new Dimension(200, 20));
scrollpane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
@Override
public void adjustmentValueChanged(AdjustmentEvent e)
{
float pos = scrollpane.getVerticalScrollBar().getValue() / (float) scrollpane.getVerticalScrollBar().getMaximum();
int rpos = (int) (pos * list.getModel().getSize());
list.setSelectedIndex(rpos);
}
});
JPanel containerPanel = new JPanel();
containerPanel.setLayout(new FlowLayout());
containerPanel.setPreferredSize(new Dimension(200, 20));
containerPanel.add(scrollpane);
首先创建一个JList,然后将其放在JScrollPane中,然后添加调整侦听器以选择唯一可见的元素。