更改滑块的数字值

时间:2016-05-13 14:55:02

标签: button javafx slider

我正在编写一个带有两个滑块的程序,这些滑块显示在各自的标签(counter1和counter2)中。 Slider2绑定到slider1,但也可以独立移动。在移动slider1(以及结果slider2)之后,我希望能够按下一个可以执行两项操作的按钮:

  1. 锁定slider1的值
  2. 更改counter2的值,使其无论slider2在哪里都开始计数(即 - 即使滑块可能为-10,counter2显示为零。然后-9为1,-8为2,等)
  3. 这是我到目前为止的相关代码:

    public CreateSliders() {
    
        Slider slider1 = new Slider();
        slider1.setMin(-30);
        slider1.setMax(30);
        slider1.setMinWidth(300);
        slider1.setValue(0);
        slider1.setShowTickLabels(true);
        slider1.setShowTickMarks(true);
        slider1.setMajorTickUnit(10);
        slider1.setMinorTickCount(1);
        slider1.setBlockIncrement(1);
    
        Label counter1 = new Label(Double.toString(slider1.getValue()));
    
        Button btn1 = new Button("Lock");
    
        HBox counter1Hbox = new HBox(counter1, btn1);
        counter1Hbox.setAlignment(Pos.CENTER);
    
        btn1.setOnAction(new EventHandler<ActionEvent>() {
            public void handle (ActionEvent event) {
                counter2.setText("0.0");                
            }
        });
    
        Slider slider2 = new Slider();
        slider2.setMin(-30);
        slider2.setMax(30);
        slider2.setMinWidth(300);
        slider2.setValue(0);
        slider2.setShowTickLabels(true);
        slider2.setShowTickMarks(true);
        slider2.setMajorTickUnit(10);
        slider2.setMinorTickCount(1);
        slider2.setBlockIncrement(1);
    
        Label counter2 = new Label(Double.toString(slider2.getValue()));
    
        HBox counter2Hbox = new HBox(counter2);
        counter2Hbox.setAlignment(Pos.CENTER);
    
        slider1.valueProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                counter1.setText(String.format("%.0f", newValue));
                slider2.valueProperty().set(slider2.valueProperty().get() + newValue.doubleValue() - oldValue.doubleValue());
            }
        });
    
        slider2.valueProperty().addListener(new ChangeListener<Number>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                counter2.setText(String.format("%.0f", newValue));
            }
        });
    }
    

1 个答案:

答案 0 :(得分:1)

要锁定滑块位置,您可以使用:

ToggleButton btnLock = new ToggleButton("Lock");
btnLock.selectedProperty().addListener((ov, b, b1) -> 
   {
      slider1.setMouseTransparent(b1));
      //or
      slider.setDisable(b1));
   });

//or
slider1.disableProperty().bind(btnLock.selectedProperty());

对于第2点:

private double lastSliderPosition;

public void createSliders(){
        //initialize sliders 

        ToggleButton btnLock = new ToggleButton("Lock");
        btnLock.selectedProperty().addListener((ov, b, b1) -> {
            slider1.setDisable(b1);

            if (b1) {
                slider2.valueProperty().unbind();
            } else {
                slider2.valueProperty().bind(slider1.valueProperty());
            }
        });

        slider2.valueProperty().bind(slider1.valueProperty());

        slider2.valueProperty().addListener((ov, t, t1) -> {
            if (!slider2.isValueChanging()) {
                lastSliderValue = (double) t1;
            }

            if (btnLock.isSelected()) {
                counter2.setText(String.format("%.0f", t.doubleValue() - lastSliderValue));
            } else {
                counter2.setText(String.format("%.0f", t1));
            }
        });
}