gpt4 book ai didi

java - 连接具有 StringConverter 的 slider 和微调器

转载 作者:搜寻专家 更新时间:2023-10-31 19:51:44 25 4
gpt4 key购买 nike

对于数字输入,有时将模拟控件同步到文本显示会很方便。在这个 Swing 中example ,一个 JSpinner 和一个 JSlider 各自监听变化事件,并且各自更新对方的模型以匹配。一个类似的 JavaFX 程序,如下所示,连接一个 Spinner和一个 Slider ,并且这些听众保持控件的协调:

slider.valueProperty().addListener((Observable o) -> {
spinner.getValueFactory().setValue(slider.getValue());
});
spinner.valueProperty().addListener((Observable o) -> {
slider.setValue((double) spinner.getValue());
});

不幸的是,当我将 StringConverter 添加到微调器的 SpinnerValueFactory 时,初始值是未格式化的,直到任一控件发生更改 — 即使再次显式设置初始值,< em>在 添加转换器之后:

spinner.getValueFactory().setConverter(…);
spinner.getValueFactory().setValue(INITIAL_VALUE);

我哪里错了?

image

import java.text.NumberFormat;
import javafx.application.Application;
import javafx.beans.Observable;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.converter.PercentageStringConverter;

/**
* @see https://stackoverflow.com/a/6067986/230513
*/
public class SpinSlider extends Application {

private static final double MIN = 0;
private static final double MAX = 1;
private static final double INITIAL_VALUE = 0.5;
private static final double STEP_INCREMENT = 0.1;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SpinSlider");
Slider slider = new Slider(MIN, MAX, INITIAL_VALUE);
slider.setBlockIncrement(STEP_INCREMENT);
Spinner spinner = new Spinner(MIN, MAX, INITIAL_VALUE, STEP_INCREMENT);
spinner.getValueFactory().setConverter(
new PercentageStringConverter(NumberFormat.getPercentInstance()));
spinner.getValueFactory().setValue(INITIAL_VALUE);
slider.valueProperty().addListener((Observable o) -> {
spinner.getValueFactory().setValue(slider.getValue());
});
spinner.valueProperty().addListener((Observable o) -> {
slider.setValue((double) spinner.getValue());
});
GridPane root = new GridPane();
root.addRow(0, slider, spinner);
root.setPadding(new Insets(8, 8, 8, 8));
root.setHgap(8);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}

}

最佳答案

您的 INITIAL_VALUE 首先用作微调器构造函数的 initialValue 参数。在内部,旋转器的混凝土 SpinnerValueFactory是一个 DoubleSpinnerValueFactory,它将 ChangeListener 添加到它的 value 属性;当再次设置初始值时,该值并没有真正改变。有两种方法:

  1. 为构造函数指定一个不同的值,并在添加转换器后设置所需的值:

    Spinner spinner = new Spinner(MIN, MAX, -42, STEP_INCREMENT);
    spinner.getValueFactory().setConverter(…);
    spinner.getValueFactory().setValue(INITIAL_VALUE);
  2. 构造一个具有所需初始值的 SpinnerValueFactory 并使用它来构造微调器:

    SpinnerValueFactory factory = new SpinnerValueFactory
    .DoubleSpinnerValueFactory(MIN, MAX, INITIAL_VALUE, STEP_INCREMENT);
    factory.setConverter(…);
    Spinner spinner = new Spinner(factory);

此外,下面的示例将两个监听器替换为双向绑定(bind),它使用弱监听器来允许对属性进行垃圾回收:

slider.valueProperty().bindBidirectional(
spinner.getValueFactory().valueProperty());

简单地说,微调器和 slider 可以相互控制。更常见的是,每个模型都可以在控制另一个模型持有的属性的过程中保持同步:

model.xProperty().bindBidirectional(slider.valueProperty());
model.xProperty().bindBidirectional(spinner.getValueFactory().valueProperty());

image

import java.text.NumberFormat;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.converter.PercentageStringConverter;

/**
* @see https://stackoverflow.com/a/55427307/230513
* @see https://stackoverflow.com/a/6067986/230513
*/
public class SpinSlider extends Application {

private static final double MIN = 0;
private static final double MAX = 1;
private static final double INITIAL_VALUE = 0.5;
private static final double STEP_INCREMENT = 0.1;

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SpinSlider");
Slider slider = new Slider(MIN, MAX, INITIAL_VALUE);
slider.setBlockIncrement(STEP_INCREMENT);
SpinnerValueFactory factory = new SpinnerValueFactory
.DoubleSpinnerValueFactory(MIN, MAX, INITIAL_VALUE, STEP_INCREMENT);
factory.setConverter(new PercentageStringConverter(
NumberFormat.getPercentInstance()));
Spinner spinner = new Spinner(factory);
slider.valueProperty().bindBidirectional(spinner.getValueFactory().valueProperty());
GridPane root = new GridPane();
root.addRow(0, slider, spinner);
root.setPadding(new Insets(8, 8, 8, 8));
root.setHgap(8);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}

}

关于java - 连接具有 StringConverter 的 slider 和微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55427306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com