JScrollBar:旋钮不可见,最大值较小
JScrollBar: the knob is not visible with small max values
我想创建一个最大值设置为 2 的水平滚动条(它应该只允许选择 0、1 或 2 作为值),但如果值小于 11,则旋钮不可见。
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);
当我将 maximum
值更改为 12 时,它按我想要的方式工作(可见旋钮,值 [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);
您要查找的可能是 JSlider
,而不是 JScrollbar
。
// 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
将 AdjustmentListener
添加到滑块,而不是 AdjustmentListener
,如下所示:
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());
}
}
});
有关 JSlider
的更多信息和官方教程,请查看 The Java™ Tutorials - How to Use Sliders
我想创建一个最大值设置为 2 的水平滚动条(它应该只允许选择 0、1 或 2 作为值),但如果值小于 11,则旋钮不可见。
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);
当我将 maximum
值更改为 12 时,它按我想要的方式工作(可见旋钮,值 [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);
您要查找的可能是 JSlider
,而不是 JScrollbar
。
// 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
将 AdjustmentListener
添加到滑块,而不是 AdjustmentListener
,如下所示:
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());
}
}
});
有关 JSlider
的更多信息和官方教程,请查看 The Java™ Tutorials - How to Use Sliders