2016-12-04 21 views
0

最大値が2に設定されている水平スクロールバーを作成したい場合(0,1または2を値として選択する必要があります)、ノブは表示されません。私は12にmaximum値を変更すると値がより小さい11JScrollBar:小さな最大値でノブが表示されない

scrlLineDist = new JScrollBar(); 
    scrlLineDist.setBlockIncrement(1); 
    scrlLineDist.addAdjustmentListener(new AdjustmentListener() { 
     public void adjustmentValueChanged(AdjustmentEvent e) { 
      System.out.println(scrlLineDist.getValue()); 
     } 
    }); 
    GridBagConstraints gbc_scrlLineDist = new GridBagConstraints(); 
    gbc_scrlLineDist.insets = new Insets(0, 0, 5, 0); 
    gbc_scrlLineDist.fill = GridBagConstraints.HORIZONTAL; 
    gbc_scrlLineDist.gridx = 0; 
    gbc_scrlLineDist.gridy = 3; 
    panel_4.add(scrlLineDist, gbc_scrlLineDist); 
    scrlLineDist.setMaximum(2); 
    scrlLineDist.setToolTipText(""); 
    scrlLineDist.setOrientation(JScrollBar.HORIZONTAL); 

であり、それは私が望むように動作(可視ノブ、値[0,2])。なぜこうなった?あなたが探しているものを

scrlLineDist = new JScrollBar(); 
    scrlLineDist.setBlockIncrement(1); 
    scrlLineDist.addAdjustmentListener(new AdjustmentListener() { 
     public void adjustmentValueChanged(AdjustmentEvent e) { 
      System.out.println(scrlLineDist.getValue()); 
     } 
    }); 
    GridBagConstraints gbc_scrlLineDist = new GridBagConstraints(); 
    gbc_scrlLineDist.insets = new Insets(0, 0, 5, 0); 
    gbc_scrlLineDist.fill = GridBagConstraints.HORIZONTAL; 
    gbc_scrlLineDist.gridx = 0; 
    gbc_scrlLineDist.gridy = 3; 
    panel_4.add(scrlLineDist, gbc_scrlLineDist); 
    scrlLineDist.setMaximum(12); 
    scrlLineDist.setToolTipText(""); 
    scrlLineDist.setOrientation(JScrollBar.HORIZONTAL); 

答えて

1

はおそらくJSlider、ないJScrollbarです。代わりにAdjustmentListener

// orientation, min, max, initial value 
final JSlider slider = new JSlider(SwingConstants.HORIZONTAL, 0, 2, 1); 
slider.setSnapToTicks(true); // only allow 0, 1, 2 and not in between 
slider.setPaintTicks(true); // paint ticks at tick spacing interval 
slider.setMajorTickSpacing(1); // set interval to 1 
slider.setPaintLabels(true); // show labels on ticks 

、そのように、あなたのスライダーにChangeListenerを追加:あなたは、The Java™ Tutorials - How to Use Sliders

+0

おっとをチェックアウトし、JSliderの詳細については、公式チュートリアルについて

slider.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { // only output when value is set (when the mouse is released from the knob) // remove this if statement if you would like output whenever the knob is moved if(!slider.getValueIsAdjusting()) { System.out.println(slider.getValue()); } } }); 

そうです。ありがとうございました! –

関連する問題