当前位置: 首页 > 知识库问答 >
问题:

无法将项目添加到组合框

公良鸿光
2023-03-14

我试图通过FXML文件将项目添加到组合框中,但出现了这个错误。

    javafx.fxml.LoadException: String is not a valid type.
/C:/Users/kim/Desktop/JavaFX/ComboboxFx/bin/application/Main.fxml:13

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.createElement(Unknown Source)
    at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

这是我的FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" >
   <children>
      <ComboBox fx:id="comboBox">
        <items>
            <FXCollections fx:factory="observableArrayList">
                <String fx:value = "Gilbert" />
                <String fx:value = "John" />
            </FXCollections>
        </items>
      </ComboBox>
      <Label fx:id="selection" layoutX="34.0" layoutY="65.0" prefHeight="17.0" prefWidth="102.0" text="Label" />
   </children>
</AnchorPane>

主要

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

当我通过scene builder打开它时,会出现这个错误

java.io.IOException: javafx.fxml.LoadException: Invalid attribute.

我已经仔细检查过了,但代码似乎没问题。当我删除试图添加到FXML的值时,错误消失了,但我的组合框变为空。它说String不是有效的类型。我怎样才能解决这个问题?为什么它是无效的?我在这里检查了类似的问题,但是错误是由于没有导入FXCollections造成的。我导入了它,所以我认为错误不是由导入错误引起的。

共有1个答案

颛孙嘉石
2023-03-14

添加

到你的fxml。

 类似资料:
  • 问题内容: 尝试从此处使用unstruction将alamofire添加到快速项目 完成所有这些步骤,清理项目很多计时器并重新启动XCode,没有任何帮助。错误不会消失 http://i.stack.imgur.com/kNU3R.png “无法为’Alamofire’加载基础模块”,无关紧要 我所做的更改: 1)将项目文件添加到我的项目 i.stack.imgur.com/eUe8E.png 2

  • 我正试图给我的maven项目添加依赖项,但我无法让它工作。 这是我的POM文件: 在构建项目时,我遇到以下错误: 未能在项目培训上执行目标解决方案:无法解析项目hva的依赖项。dmci。信息和通信技术。ads:TrainsSolution:jar:1.0-SNAPSHOT:未能收集[org.junit.jupiter:junit-jupiter:jar:5.4.0-M1(测试)]的依赖项:未能读取

  • 问题内容: 我需要完成以下任务: 从: (向每行添加一个元素)到: 我已经尝试做类似a [n] = array([1,3,4,x])的事情 但是numpy抱怨形状不匹配。我尝试遍历并将元素x附加到每个项目,但是更改未反映出来。 关于如何实现此目标的任何想法? 问题答案: 对于有python经验的人来说,将数据追加到现有数组是很自然的事情。但是,如果您发现自己定期附加到大型数组,则会很快发现NumP

  • 我试图向mongodb发送一个数组,但是返回一个空的,而且mongodb文档中从来没有出现biddingGroup字段。我看过堆栈文章,并看到了关于模式的建议。我试过了 应用程序JS 感谢任何帮助。

  • 我正在尝试添加卡片视图,但当我使用此依赖项添加库时:我收到以下错误: 错误:任务“:app:processDebugManifest”的执行失败。清单合并失败:属性元数据#android。支持VERSION@value值=(26.0.0-alpha1)来自[com.android.support:appcompat-v7:26.0.0-alpha1]AndroidManifest。xml:27:9

  • 所以这对我来说有点新,我使用adblock项目,从webviewer显示我的网站,但我不想显示我的网站的广告到它,所以我使用adblock项目相同。但不知何故,我犯了一个奇怪的错误