簡體   English   中英

如何將JSlider選擇值設置為全局變量?

[英]How To Set JSlider Selected Value To Global Variable?

有誰知道我將如何在JSlider, stateChanged的事件處理程序中發送選定的值並將其設置為全局變量?

例如

public class Slider extends JPanel {

    public JSlider slider;
    public UserFrame frame;
    public double[] priceRange;

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        new Slider();
    }

    public Slider() throws ClassNotFoundException, SQLException {
        slider = new JSlider();
        priceRange = new double[2];

        slider.setBorder(BorderFactory.createTitledBorder("Maximum Price in SEK"));

    /*
     * The minimum and maximum values are queried from the database. Those
     * values are then rounded up (maximum) to the nearest 10 SEK.
     * For example, if the minimum is 67 SEK and the maximum 95 SEK, then
     * the displayed range is [70, 100].
     */
        int min = (getMinimumPrice() / 10) * 10 + 10;
        int max = (getMaximumPrice() / 10) * 10 + 10;

        slider.setMajorTickSpacing(10);
        slider.setMinorTickSpacing(5);
        slider.setMinimum(min);
        slider.setMaximum(max);
        slider.setPaintTicks(true);
        slider.setPaintLabels(true);
        add(slider);
    }

    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        if (!source.getValueIsAdjusting()) {
            slider.getModel().setValue(slider.getModel().getValue());
            priceRange[0] = slider.getModel().getValue();
        }
    }
}

...依此類推,忽略方法getMinimumgetMaximum它們只是檢索數據庫中的最高和最低值,但是可以說,當用戶釋放JSlider旋鈕時,我想做的就是將其設置為priceRange[0] 我將如何做到這一點? 我嘗試創建一個模型並設置每次使用stateChanged設置的時候,我在主要代碼集中的actionListener中也都設置了類似的內容...

priceRange[0] = slider.getModel().getValue();

然后打印出該結果並獲得我設置的最小值。

我真的很感謝任何進一步的建議。

祝您假期愉快! :)

祝一切順利,馬庫斯

只需刪除以下行:

slider.getModel().setValue(slider.getModel().getValue());
priceRange[0] = slider.getModel().getValue();

並插入此代碼:

priceRange[0] = (double)slider.getValue();

祝好運!

PS您在哪里實現了ChangeListener接口?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM