如何将事件添加到此代码中,以便在拖动滑块时显示该数字。请让我知道,因为我是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);
    }
}

最佳答案

作为使用ChangeListener的具体示例,SpinSlider显示了如何连接JSliderJSpinner

10-07 16:45