JavaFX控件微调器不起作用

时间:2019-04-09 22:35:10

标签: java intellij-idea javafx javafx-11

嗨,我想添加1至24之间的“ Spinner”整数控件。我编写了函数,并添加到fxml文件中,但显示异常(JavaFX 11)。

通常,当我使用默认值“ 7”构建窗口显示微调器时,我也可以将int从1更改为24,但是每次单击都会显示此异常。 总结

addChangeVIEW.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="305.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.addChangeController">
   <children>
      <Button fx:id="addChangeButton" graphicTextGap="8.0" layoutX="413.0" layoutY="110.0" mnemonicParsing="false" prefHeight="85.0" prefWidth="128.0" text="ADD CHANGE" textAlignment="CENTER" textFill="#e50a0a" textOverrun="LEADING_WORD_ELLIPSIS" />
      <DatePicker fx:id="dateChangePicker" layoutX="23.0" layoutY="138.0" />
      <Label fx:id="descDatePicker" layoutX="70.0" layoutY="100.0" prefHeight="18.0" prefWidth="89.0" text="Date Change">
         <font>
            <Font size="14.0" />
         </font>
      </Label>
      <Label fx:id="fromHourLabel" layoutX="215.0" layoutY="99.0" text="From Hour" />
      <Label fx:id="toHourLabel" layoutX="311.0" layoutY="99.0" text="To Hour" />
      <Label fx:id="statusLabel" layoutX="83.0" layoutY="216.0" prefHeight="33.0" prefWidth="258.0" text="WAIT FOR ADD CHANGE" textFill="#14f004">
         <font>
            <Font name="Arial" size="18.0" />
         </font>
      </Label>
      <Spinner fx:id="fromHourSpinner" layoutX="214.0" layoutY="137.0" onMouseClicked="#spinnersAction" prefHeight="26.0" prefWidth="58.0" />
      <Spinner fx:id="toHourSpinner" accessibleText="1" layoutX="303.0" layoutY="137.0" onMouseClicked="#spinnersAction" prefHeight="26.0" prefWidth="58.0" />
   </children>
</AnchorPane>
addChangeController

package com.controller;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;

import java.net.URL;
import java.util.ResourceBundle;

public class addChangeController implements Initializable {


    @FXML
    private Spinner<Integer> fromHourSpinner;

    @FXML
    private Spinner<Integer> toHourSpinner;


    SpinnerValueFactory<Integer> spinner = new SpinnerValueFactory.IntegerSpinnerValueFactory(1,24,7);

    @FXML
    public void spinnersAction(ActionEvent event){
        //fromHourSpinner.setValueFactory(spinner);

    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        fromHourSpinner.setValueFactory(spinner);
    }
}
Init
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch

Process finished with exit code 0

1 个答案:

答案 0 :(得分:1)

由于我有同样的问题,所以这是我找到的解决方法

只需要像这样重新排列一下代码即可。

 @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
SpinnerValueFactory<Integer> spinner =new SpinnerValueFactory.IntegerSpinnerValueFactory(1,24,7);
        fromHourSpinner.setValueFactory(spinner);
        toHourSpinner.setValueFactory(spinner);
    }