我试图从scenebuilder中获取一个接口,在我的javafx项目中工作。我想知道为什么我在运行时加载程序时遇到问题。
这是我的申请文件
package com.example.javafx_project;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
/**
*
*/
@FXML
private VBox vbox;
@FXML
private Stage stage;
@FXML
private BorderPane borderpane;
@Override
public void start(Stage stage) throws IOException {
this.stage = stage;
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("thisView.fxml"));
this.stage.setTitle("This is it");
Scene scene = new Scene(fxmlLoader.load(), 600, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
字符串
我的控制器:
package com.example.javafx_project;
import java.awt.Button;
import java.awt.Label;
import javafx.fxml.FXML;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
public class MainDisplayController {
@FXML
private Button button;
@FXML
private BorderPane pane;
@FXML
private ToolBar toolbar;
@FXML
private Label welcomeText;
@FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
型
我的fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.javafx_project.MainDisplayController">
<top>
<ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Label fx:id="welcomeText"/>
<Button mnemonicParsing="false" text="Button" />
</items>
</ToolBar>
</top>
</BorderPane>
型
我估计它应该显示GUI,但它没有显示任何东西。
我已经尝试改变应用程序加载器来保存thisView.fxml文件,但它似乎没有加载我在scenebuilder中创建的GUI。不知道为什么。
堆栈跟踪:
javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 20.0.1 by JavaFX runtime of version 20
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: javafx.fxml.LoadException: Label is not a valid type.
/C:/Users/Diane/java-project/SWE3313Proj/JavaFX_Project%20-%20Copy/target/classes/com/example/javafx_project/thisView.fxml:13
at javafx.fxml@20/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2718)
at javafx.fxml@20/javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2920)
at javafx.fxml@20/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2850)
at javafx.fxml@20/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2649)
at javafx.fxml@20/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2563)
at javafx.fxml@20/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
at com.example.javafx_project/com.example.javafx_project.HelloApplication.start(HelloApplication.java:29)
at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
at javafx.graphics@20/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at javafx.graphics@20/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@20/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
... 1 more
Exception running application com.example.javafx_project.HelloApplication
型
1条答案
按热度按时间vaqhlq811#
解决方案
你用错了进口货。
字符串
您必须使用JavaFX按钮和JavaFX标签,因为您正在使用JavaFX而不是Java AWT/Swing进行开发。