【发布时间】: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调用。 -
如何在拖动滑块时让文本字段为空?