【问题标题】:JavaFX Update Textfield with Numeric ValuesJavaFX 使用数值更新文本字段
【发布时间】:2016-08-30 22:30:44
【问题描述】:

我正在尝试使用数值更新文本字段,但似乎不知道该怎么做。我试图在文本字段中添加的值是滑块上位置的值,当您将其向后和第四次移动时会更新文本字段。任何类型的帮助将不胜感激。我不确定你们是否希望我发布我的代码,但它只是一个 1-10 的滑块和一个网格窗格中的空白文本字段,所以它没有多大帮助。

public class Main extends Application {

private static Slider fibSlider = new Slider(0,10,0);
private static Label indexLabel = new Label("Index: ");
private static int colIndex = 0;
private static int rowIndex = 0; 
private static int topIndex = 0;
private static int rightIndex = 0;  
private static int leftIndex = 0;
private static int bottomIndex = 0;
private static TextField tfIndex;

@Override
public void start(Stage primaryStage) {


    fibSlider.setMajorTickUnit(1);
    fibSlider.setMinorTickCount(0);
    fibSlider.setShowTickLabels(true);
    fibSlider.setShowTickMarks(true);

/*  fibSlider.valueProperty().addListener(sl -> {
            tfIndex.setText(fibSlider.getValue());
        });
    */
    GridPane mainGPane = buildGPane();
    Scene mainScene = new Scene(mainGPane, 500, 200);

    primaryStage.setTitle("ex");
    primaryStage.setScene(mainScene);
    primaryStage.show();


}

 public static GridPane buildGPane() {
     GridPane gPane = new GridPane();
     gPane.setAlignment(Pos.CENTER);
        gPane.setPadding(new Insets(topIndex=10,rightIndex=10,
           bottomIndex=10,leftIndex=10));
        gPane.setHgap(2);
        gPane.setVgap(2);
        gPane.add(fibSlider,colIndex=1,rowIndex=3);
        gPane.add(indexLabel,colIndex=1,rowIndex=5);
        gPane.add(tfIndex,colIndex=2,rowIndex=5);

        return gPane;

 }
 public Main() {
        tfIndex = new TextField();
 }

【问题讨论】:

  • 请显示一些代码
  • 好的,我刚刚添加了一些代码,基本上我想要做的是让文本字段使用滑块值进行更新。
  • 问题到底出在哪里?取消注释监听器并在fibSlider.getValue() 周围添加一个Double.toString 调用。
  • 如何在拖动滑块时让文本字段为空?

标签: java javafx


【解决方案1】:

您的代码示例存在一些问题,因为您将成员定义为静态成员,这在 OO 编程中不是很好的形式。进一步,当您尝试初始化其中一些静态 JavaFX 控件时,您会遇到异常,因为 Toolkit 尚未初始化,至少您使用最新版本的 JVM。

诀窍是将tfIndex 的文本属性绑定到fibSlider 的值。由于该值的类型为Double,并且对于绑定,您实际上需要StringProperty,您可以将DoubleProperty 转换为StringProperty

tfIndex.textProperty().bind(fibSlider.valueProperty().asString());

这是完整的例子:

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Main extends Application {

    private Slider fibSlider = new Slider(0, 10, 0);
    private Label indexLabel = new Label("Index: ");
    private int colIndex = 0;
    private int rowIndex = 0;
    private int topIndex = 0;
    private int rightIndex = 0;
    private int leftIndex = 0;
    private int bottomIndex = 0;
    private TextField tfIndex;

    @Override
    public void start(Stage primaryStage) {

        tfIndex = new TextField();

        fibSlider.setMajorTickUnit(1);
        fibSlider.setMinorTickCount(0);
        fibSlider.setShowTickLabels(true);
        fibSlider.setShowTickMarks(true);

        tfIndex.textProperty().bind(fibSlider.valueProperty().asString());

        GridPane mainGPane = buildGPane();
        Scene mainScene = new Scene(mainGPane, 500, 200);

        primaryStage.setTitle("ex");
        primaryStage.setScene(mainScene);
        primaryStage.show();


    }

    private GridPane buildGPane() {
        GridPane gPane = new GridPane();
        gPane.setAlignment(Pos.CENTER);
        gPane.setPadding(new Insets(topIndex = 10, rightIndex = 10,
                bottomIndex = 10, leftIndex = 10));
        gPane.setHgap(2);
        gPane.setVgap(2);
        gPane.add(fibSlider, colIndex = 1, rowIndex = 3);
        gPane.add(indexLabel, colIndex = 1, rowIndex = 5);
        gPane.add(tfIndex, colIndex = 2, rowIndex = 5);

        return gPane;

    }
}

【讨论】:

  • 非常感谢您的回复,我实际上使用了诸如(抱歉不确定如何放入块) fibSlider.valueProperty().addListener(sl -> { tfIndex.setText(Double. toString(fibSlider.getValue())); });
  • 又一个问题,如何在拖动滑块时让文本字段为空?
  • 有属性valueChange 表明了这一点。如果您只想在滑块不变时显示滑块的值,我建议在该属性上设置一个侦听器,并在值变为 false 时设置文本。
猜你喜欢
  • 2018-07-13
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多