简体   繁体   English

JSlider值仅在移动滑块时显示

[英]JSlider value only showing upon moving slider

So im using a JSlider to display rain amount in mm,but the rain amount pops up once I move the slider,I want it to be present before I move the slider. 因此,我使用JSlider以毫米为单位显示雨量,但是一旦移动滑块,雨量就会弹出,我希望在移动滑块之前先出现雨量。 Here are the code segments from my program. 这是我程序中的代码段。

    slider = new JSlider(JSlider.HORIZONTAL, 0 ,100,0);
    slider.setBounds(70, 70,120,35);
    int sliderValue = slider.getValue();
    event e = new event();
    slider.setMajorTickSpacing(5);
    slider.setPaintTicks(true);
    slider.setBounds(120, 200, 100, 300);


public  class event implements ChangeListener{ // For Slider

    public void stateChanged(ChangeEvent e) {

        int SliderValue = slider.getValue();

        label3.setText("Rain ammount: "+SliderValue+"mm");

    }
}

Although it is not entirely clear what the last problem was, here the answer from the comments, in order to give this question the chance to be moved out of the "unanswered" list: 尽管尚不完全清楚最后一个问题是什么,但是这里是注释的答案,以便使此问题有机会移出“未答复”列表:

You can probably just create your label3 as 您可能只需将label3创建为

label3 = new JLabel("Rain ammount: 0mm");

Initially, the slider value is 0. So the text on the label will have to be "Rain ammount: 0mm" . 最初,滑块的值为0。因此标签上的文本必须为"Rain ammount: 0mm" When you change the slider (so that its value is no longer 0), then the label will be updated anyhow. 更改滑块(使其值不再为0)时,标签将以任何方式更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM