JavaFX使用数值更新文本字段

时间:2016-05-05 08:00:28

标签: java javafx

我尝试使用数值更新文本字段,但似乎无法弄清楚如何操作。我试图在文本字段中添加的值是滑块上的位置值,在向后移动时更新文本字段和第四个。任何类型的帮助将不胜感激。我不确定你们是否要我发布我的代码,但它只有一个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();
 }

1 个答案:

答案 0 :(得分:1)

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

诀窍是将tfIndex的text属性绑定到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;

    }
}