本文介绍了获取JSlider的输入和更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用JSlider来实时获取输入,这意味着它将在不按任何按钮的情况下返回输入.我有滑块的这段代码:
I need to use JSlider with getting input live, meaning that it will return an input without pressing any button. I have this piece of code for the slider:
JPanel panel = new JPanel();
JSlider js = new JSlider(JSlider.VERTICAL, 0, 20, 10);
js.setMajorTickSpacing(2);
js.setPaintTicks(true);
Hashtable labelTable = new Hashtable();
labelTable.put(new Integer(js.getMinimum()), new JLabel("x0"));
labelTable.put(new Integer((js.getMinimum() + js.getMaximum()) / 2), new JLabel("x1"));
labelTable.put(new Integer(js.getMaximum()), new JLabel("x2"));
js.setLabelTable(labelTable);
js.setPaintLabels(true);
panel.add(js);
int result = JOptionPane.showConfirmDialog(null, panel, "choose size", JOptionPane.YES_OPTION);
是否有可能这样做?我以为可以使用actionListener,但是没有成功.
Is it even possible to do so? I thought using actionListener but I didn't succeed.
推荐答案
例如,可以使用ChangeListener
It's possible, using a ChangeListener, for example
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
JSlider slider = (JSlider) e.getSource();
int value = slider.getValue();
...
}
});
这篇关于获取JSlider的输入和更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!