gpt4 book ai didi

java - FXMLLoader.getController() 无论如何都返回 null

转载 作者:太空宇宙 更新时间:2023-11-04 12:32:14 26 4
gpt4 key购买 nike

我一直在为我的 JavaFX 学习做一个小应用程序。在应用程序中,我需要从主文件中单独获取 Controller ,所以我设计了这个:

public class DiceObserver implements Observer {

FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
public Controller controller = loader.<Controller>getController();

Dice ov;
public DiceObserver(Dice dice){
this.ov = dice;
}

public void update(Observable o, Object arg) {
int roll = ov.getLastValue().getRolledValue();
System.out.println(roll);
controller.updateBarChart(roll);
}
}

FXML 文件:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

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


public class Controller{
@FXML public TextField freqText;
@FXML public Slider freqSlider;
@FXML public Button startButton;
@FXML public Button endButton;
@FXML public BarChart rollChart;
@FXML public ImageView dicePic;
XYChart.Series s1 = new XYChart.Series<>();
XYChart.Series s2 = new XYChart.Series<>();
XYChart.Series s3 = new XYChart.Series<>();
XYChart.Series s4 = new XYChart.Series<>();
XYChart.Series s5 = new XYChart.Series<>();
XYChart.Series s6 = new XYChart.Series<>();
Image side1 = new Image("Dice_Side_1.png");
SimulationThread t = SimulationThread.getInstance();

public void startSimulation(ActionEvent event) { // start the simulation
t.start();
}

public void getNewFreqValue(ActionEvent event) {
freqText.setText(String.valueOf(freqSlider.getValue()));
}

public void stopSimulation(ActionEvent event) {
t.end();
}

public void updateBarChart(int diceRoll){
System.out.println(diceRoll);
int rollval = diceRoll;
switch (rollval) {
case 1:
s1.getData().add(rollval);
break;
case 2:
s2.getData().add(rollval);
break;
case 3:
s3.getData().add(rollval);
break;
case 4:
s4.getData().add(rollval);
break;
case 5:
s5.getData().add(rollval);
break;
case 6:
s6.getData().add(rollval);
break;
}
}

public void updateDicePic(int diceRoll){
dicePic.setImage(side1);
}

public void initialize() {
}
}

updateBarChart 方法位于 Controller 中。当我运行代码时,加载器有一个值,但 getcontroller 方法返回 null。我该如何解决这个问题?

最佳答案

它返回null,因为您尚未指定 Controller 。从您上面发布的代码来看,您的 Controller 和 FXML 彼此不知道。

调用:

loader.setController(yourControllerInstance);

示例:

https://github.com/SnakeDoc/superD/blob/master/src/com/vanomaly/superd/Main.java#L39

或者您必须在 FXML 文档(在根元素中)中指定 Controller :

<AnchorPane maxHeight="...".... fx:controller="sample.Controller">
<HBox .... >
....

示例:

https://github.com/SnakeDoc/Narvaro/blob/master/src/resources/Narvaro.fxml#L13

关于java - FXMLLoader.getController() 无论如何都返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37737083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com