将场景构建器实现到javafx项目中

问题描述 投票:0回答:1

我正在尝试从在我的 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   
java html maven javafx scenebuilder
1个回答
0
投票

解决方案

您使用了错误的导入。

Wrong:
import java.awt.Button;
import java.awt.Label;

您必须使用 JavaFx 按钮和 JavaFX 标签,因为您正在使用 JavaFx 而不是 Java AWT/Swing 进行开发。

© www.soinside.com 2019 - 2024. All rights reserved.