java JavaFX 8微调器控件不更新显示的值
我是Java编程新手,选择用JavaFX8开发我的工具UI。我需要一个微调器(JavaFX8u40中的新功能!)用户可以为其指定0到120秒的整数值(简单部分),但也可以指定一个按钮,该按钮允许用户将微调器值直接设置为给定的预设按钮值(此处为0)
问题是,当我单击按钮进行测试时,微调器值似乎没有更新,并保持在60(微调器初始值)或用户通过微调器箭头控件更改的任何其他值
我从搜索解决方案中了解到,这是因为我应该操纵工厂值,而不是微调器值,但我得到了相同的结果
以下是我创建的FXML文件的代码,以反映问题:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="TestViewController">
<children>
<BorderPane layoutX="28.0" layoutY="60.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<children>
<Label mnemonicParsing="true" text="Clear Automatically _History After">
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets right="5.0" />
</padding>
</Label>
<Spinner fx:id="spinnerClearHistoryDuration" initialValue="60" max="120" min="0" prefHeight="25.0" prefWidth="60.0" />
<Button fx:id="btnClearHistoryDuration" onAction="#clickBtnClearHistoryDuration" text="_0" />
</children>
</HBox>
</center>
</BorderPane>
</children>
</AnchorPane>
下面是匹配的控制器Java类:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Spinner;
public class TestViewController implements Initializable {
@FXML
private Spinner<Integer> spinnerClearHistoryDuration;
@FXML
private Button btnClearHistoryDuration;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL location, ResourceBundle resources) {
spinnerClearHistoryDuration = new Spinner(0, 120, 30);
}
@FXML
void clickBtnClearHistoryDuration(ActionEvent event) {
//Tried the following, which is more extended but gets to same result
//(supposedly because this is just using more detailed resources but the same approach)
/*
SpinnerValueFactory<Integer> valueFactory = spinnerClearHistoryDuration.getValueFactory();
valueFactory.setValue(0);
spinnerClearHistoryDuration.setValueFactory(valueFactory);
*/
spinnerClearHistoryDuration.getValueFactory().setValue(0);
}
}
当然,我的主要方法如下:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class JavaFXTestApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("TestView.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
由于我还是这个平台的新手,我不允许发布屏幕截图来进一步说明,对此表示抱歉
任何关于我忘记或遗漏了什么的想法都将不胜感激,因为FXML8非常新,并且没有找到与我的问题相关的解决方案
所有最好的人, 蒂埃里
# 1 楼答案
问题是您要实例化两次微调器
为此:
加载FXML文件时,微调器已实例化
现在,在此之后,创建一个新实例:
解决方案:只需删除第二个实例: