我想在我的javafx项目中添加一个微调器,包括maxmimum(1)和minimum(5)。
问题是initialize方法没有调用。 什么时候应该调用初始化?为什么不呢?
这是App.java代码:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class AverageApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我的FXMLController.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.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.input.*;
public class FXMLDocumentController implements Initializable{
@FXML private Spinner spi;
@Override
public void initialize(URL url, ResourceBundle rb) {
System.out.println("HI");
this.spi.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 5));
}
}
这是我的FXMLDocument.fxml代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.web.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="714.0" prefWidth="584.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<children>
<AnchorPane layoutX="108.0" layoutY="30.0">
<children>
<Spinner fx:id="spi" />
</children>
</AnchorPane>
<Label layoutX="278.0" layoutY="34.0" text="تعداد واحد :" />
<Label layoutX="510.0" layoutY="34.0" text="نام درس :" />
<TextField alignment="CENTER" layoutX="380.0" layoutY="30.0" nodeOrientation="LEFT_TO_RIGHT" prefHeight="25.0" prefWidth="120.0" />
<Button layoutX="34.0" layoutY="98.0" mnemonicParsing="false" text="اضافه کردن" />
<Label layoutX="520.0" layoutY="123.0" text="Label" />
<Label layoutX="520.0" layoutY="162.0" text="Label" />
</children>
</AnchorPane>
答案 0 :(得分:0)
基于@fabian,您的FXML
遗漏了以下内容:
fx:controller="yourpackagename.FXMLDocumentController ">
它应该是根节点的一部分:
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="714.0" prefWidth="584.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="yourpackagename.FXMLDocumentController ">