这是我的课程,我写了一个班轮代码,将fxml文件链接到我正在处理的项目中:
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
public class towerOfHanoi extends Application implements Initializable
{
public static void main(String[] args)
{
launch(args);
}
public void start(Stage primaryStage) throws Exception
{
try
{
Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
Scene s=new Scene(rootContainer);
primaryStage.setScene(s);
//primaryStage.setTitle("Towers Of Hanoi");
primaryStage.show();
}
catch(IOException e)
{
// e.printStackTrace();
}
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
}
任何时候我试图在日食中运行它,没有任何反应。要终止的正方形是红色,好像发生了什么,但我在scenebuilder中创建的UI并没有显示。
****更新: 当我取消注释该段代码时,这是完整的堆栈跟踪:
Mar 14, 2017 4:01:49 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.102 by JavaFX runtime of version 8.0.101
javafx.fxml.LoadException: No controller specified.
/F:/2nd%20Year%20College%20Stuff/Semester%202/Event-driven%20Programming/2nd%20Year%20Workspace/CA%202/bin/application/userInterface.fxml:44
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.towerOfHanoi.start(towerOfHanoi.java:29)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:4)
正如错误告诉您:您需要 将 控制器关联到.fxml,如下所示:
FILE userInterface.fxml (您可以使用另一个容器而不是BorderPane,但它必须是相同的,它必须位于根父级上)
DefaultListableBeanFactory.preInstantiateSingletons()
你可以将Controller设置为fxml(见上文)或由SceneBuilder设置:
另外,将启动器和控制器分开会更清楚:
FILE towerOfHanoi.java
<BorderPane fx:id="background" fx:controller="application.Controller">
//Content ...
</BorderPane>
FILE Controller.java
public class towerOfHanoi extends Application{
public static void main(String[] args){
launch(args);
}
public void start(Stage primaryStage) throws Exception{
try{
Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
Scene s=new Scene(rootContainer);
primaryStage.setScene(s);
//primaryStage.setTitle("Towers Of Hanoi");
primaryStage.show();
}
catch(IOException e){
// e.printStackTrace();
}
}
}