gpt4 book ai didi

java - JScrollBar:最大值较小时旋钮不可见

转载 作者:行者123 更新时间:2023-12-01 09:10:12 27 4
gpt4 key购买 nike

我想创建一个水平滚动条,其最大值设置为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);

当我将最大值值更改为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

向 slider 添加一个 ChangeListener,而不是 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

关于java - JScrollBar:最大值较小时旋钮不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40962948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com