2016-11-20 102 views
0

我在寻找一个Swing组件,它结合了JList的外观和JSpinner提供的上下功能,该功能也响应滚动。有谁知道这样的事情,如果没有,我该如何制作我自己的Swing组件?我需要它的一个游戏,我试图让...Java是否有类似JRoulette组件的东西?

回答

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内,然后你添加调整侦听器以选择唯一可见的元素。

Showcase

+0

这是有点hacky,但它的工作原理。 – 493msi

+0

我想我会试试这个。非常感谢,亲切的陌生人! – ArcIX

相关问题