質問

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. 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 = new JLabel("Rain ammount: 0mm");

Initially, the slider value is 0. So the text on the label will have to be "Rain ammount: 0mm". When you change the slider (so that its value is no longer 0), then the label will be updated anyhow.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top