拜托,我试图添加一个changeHandler事件,但是我认为我做对了。我正在查看以下语句。
pickNum.addChangeListener(new ChangeHandler());
@Override
public void stateChanged(changeEvent e)
{
JSlider s = (JSlider)e.getSource();
index = s.getValue();
}
有什么我可以做得更好的事情吗,因为它没有用..
import javax.swing.*;
public class Slider extends JFrame{
public static int index;
JSlider pickNum = new JSlider(JSlider.HORIZONTAL,0,30,5);
public Slider()
{
super("Slider");
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pickNum.setMajorTickSpacing(10);
pickNum.setMinorTickSpacing(1);
pickNum.setPaintTicks(true);
pickNum.setPaintLabels(true);
pickNum.addChangeListener(new ChangeHandler());
@Override
public void stateChanged(changeEvent e)
{
JSlider s = (JSlider)e.getSource();
index = s.getValue();
}
getPointedValue();
this.add(pickNum);
this.setVisible(true);
}
public final int getPointedValue()
{
int value;
value = pickNum.getValue();
return value;
}
public static void main(String[] args) {
Slider frame = new Slider();
System.out.println("value is :"+Slider.index);
}
}
[/CODE]
最佳答案
你的ChangeHandler是什么?您尚未发布该代码
这是使用变更监听器的示例
JSlider s = new JSlider();
s.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
System.out.println("Changed: " + e);
}
});