【发布时间】:2012-12-22 16:14:37
【问题描述】:
有谁知道我将如何在 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();
}
}
}
... 等等,忽略 getMinimum 和 getMaximum 方法,它们只是在我的数据库中检索最高和最低值,但是可以说,当用户释放 JSlider 旋钮时,我想要发生的所有事情就是将其设置为priceRange[0]。我怎么能做到这一点?我已经尝试创建一个模型并设置它,每次使用 stateChanged 我也在我的主要代码集中的 actionListener 之外分配类似的东西......
priceRange[0] = slider.getModel().getValue();
然后打印出该结果并获得我设置的最小值。
非常感谢任何进一步的建议。
祝你即将到来的假期愉快! :)
一切顺利, 马库斯
【问题讨论】:
标签: java swing global-variables return jslider