如何将事件添加到我的滑块代码中

时间:2012-03-28 16:21:09

标签: java swing jslider changelistener

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

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:3)

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

Spin slider