我是 Java 编程新手,并选择在 JavaFX8 中开发我的工具 UI。我需要一个微调器(JavaFX8u40 中的新功能!),用户可以为其指定 0 到 120 秒之间的整数值(简单的部分),而且还需要一个按钮,允许用户将微调器值直接设置为给定的预置值。设置按钮值(此处为 0)。
问题是,当我单击按钮进行测试时,微调器值似乎没有更新,并保持在 60(微调器初始值)或用户可能通过微调器箭头控件更改的任何其他值.
从我对解决方案的搜索中我了解到这是因为我应该操纵工厂值而不是微调器值本身,但我得到了相同的结果。
以下是我为反射(reflect)该问题而创建的 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>
这是匹配的 Controller 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 相当新,并且没有找到与我的问题有些相关的解决方案。
所有最好的人, 蒂埃里
最佳答案
问题是您正在实例化两次微调器。
这样:
@FXML
private Spinner<Integer> spinnerClearHistoryDuration;
加载 FXML 文件时,微调器已实例化。
现在,在此之后,您创建一个新实例:
@Override
public void initialize(URL location, ResourceBundle resources) {
spinnerClearHistoryDuration = new Spinner(0, 120, 30);
}
解决方案:只需删除第二个实例:
@Override
public void initialize(URL location, ResourceBundle resources) {
}
关于JavaFX 8 Spinner 控件不更新显示的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421788/