如何向此代码添加事件,以便在拖动滑块时显示该数字。请让我知道,因为我是Java新手。
import javax.swing.*;
public class Slider extends JFrame {
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);
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();
int i;
i = frame.getPointedValue();
System.out.println("current value is:" + i);
}
}