gpt4 book ai didi

java - 如何将JSlider选定的值设置为全局变量?

转载 作者:行者123 更新时间:2023-12-02 07:22:45 25 4
gpt4 key购买 nike

有谁知道我如何能够在 JSlider 的事件处理程序中发送选定的值,stateChanged 并将其设置为全局变量?

例如

public class Slider extends JPanel {

public JSlider slider;
public UserFrame frame;
public double[] priceRange;

public static void main(String[] args) throws ClassNotFoundException, SQLException {
new Slider();
}

public Slider() throws ClassNotFoundException, SQLException {
slider = new JSlider();
priceRange = new double[2];

slider.setBorder(BorderFactory.createTitledBorder("Maximum Price in SEK"));

/*
* The minimum and maximum values are queried from the database. Those
* values are then rounded up (maximum) to the nearest 10 SEK.
* For example, if the minimum is 67 SEK and the maximum 95 SEK, then
* the displayed range is [70, 100].
*/
int min = (getMinimumPrice() / 10) * 10 + 10;
int max = (getMaximumPrice() / 10) * 10 + 10;

slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setMinimum(min);
slider.setMaximum(max);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
add(slider);
}

public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
slider.getModel().setValue(slider.getModel().getValue());
priceRange[0] = slider.getModel().getValue();
}
}
}

...等等,忽略方法 getMinimumgetMaximum 它们只是检索数据库中的最高和最低值,但可以说我想要发生的一切当用户释放 JSlider 旋钮时,请将其设置为 priceRange[0]。我怎样才能做到这一点?我尝试创建一个模型,并设置每次使用 stateChanged 时,我也在我的主要代码集中的 actionListener 中分配类似这样的内容...

priceRange[0] = slider.getModel().getValue();

然后打印出该结果并获得我也设置的最小值。

我非常感谢任何进一步的建议。

祝您即将到来的假期愉快! :)

祝一切顺利,马库斯

最佳答案

只需删除这些行:

slider.getModel().setValue(slider.getModel().getValue());
priceRange[0] = slider.getModel().getValue();

然后插入这个:

priceRange[0] = (double)slider.getValue();

祝你好运!

附注您在哪里实现了 ChangeListener 接口(interface)?

关于java - 如何将JSlider选定的值设置为全局变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004404/

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