我是Java编程的新手,因此选择在JavaFX8中开发工具UI。我需要一个Spinner(JavaFX8u40中的新功能!),用户可以为它指定一个介于0到120秒之间的整数值(最简单的部分),还有一个按钮,它允许用户将Spinner值直接设置为给定的pre-设置按钮值(此处为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是相当新的东西,并且没有找到与我的问题相关的解决方案,因此我对遗忘或错过的任何想法都将不胜感激。
大家好,蒂埃里
问题是您实例化微调器两次。
有了这个:
@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) {
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句