繁体   English   中英

[JavaFX-Scenebuilder][自定义控制器] Scenebuilder 中的加载错误

[英][JavaFX-Scenebuilder][Custom Controller] Load error in Scenebuilder

我是stackoverflow的新手,所以如果我犯了一些错误,请不要犹豫,告诉我!

我最近一直在做一个个人项目,想创建一个带有特定小部件的 GUI。 我正在使用 JavaFX 及其 GUI:scenebuilder 在 Java 中工作。 到目前为止,由于这里提出的其他问题,我已经能够自己克服所有问题。 这个是关于使用场景构建器将我自己的 controller 添加到我的项目中。 我已经创建了我的 controller(一个简单的按钮,带有重叠的 ImageView github 链接)使用找到的Z20F38Z35E630DAF39444链接。 正如您可以测试的那样,此示例应用程序运行良好。 之后,我导出为 jar 并尝试将其添加到场景构建器中。

这是我的问题:如果我不加载(IconedButton.java 的第 32 行),我可以在 scenebuilder 中看到我的控件(没有任何显示,但它出现在列表中)但是如果我加载它,它就会消失并且我不能在scenebuilder上看到它。 我假设我的问题来自我的 fxml 文件,但它在 scenebuilder 之外正确加载,我已经尝试了我发现的关于在 scenebuilder 中添加自定义控制器的所有内容。 我想在另一个项目上使用它,也许将它添加为 jar 不是正确的方法,但我尝试一步一步地做事情以了解一切。

我希望我在这篇文章中没有做错任何事情,并相信有人可以帮助我!

在这里找到我的代码: github 链接这里是我遵循的教程

编辑:

App.java:

package iconbutton;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


/**
 * JavaFX App
 */
public class App extends Application {

    @Override
    public void start(Stage stage) {
        var javaVersion = SystemInfo.javaVersion();
        var javafxVersion = SystemInfo.javafxVersion();

        IconedButton iconedbutton = new IconedButton();
        iconedbutton.setText("Hello!");
        
        stage.setScene(new Scene(iconedbutton));
        stage.setTitle("Iconed Button");
        stage.setWidth(300);
        stage.setHeight(200);
        stage.show();
    }

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

}

IconedButton.java

package iconbutton;



import java.io.IOException;

import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;

/**
 * Sample custom control hosting a text field and a button.
 */
public class IconedButton extends AnchorPane {
    @FXML protected Button button;
    @FXML protected ImageView icon;

    public IconedButton() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/IconedButton.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        
        try {
            fxmlLoader.load();            
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
    
    public String getText() {
        return button.getText();
    }
    
    public void setText(String value) {
        button.setText(value);
    }
    
    public void setIcon(Image img) {
        icon.setImage(img);
    }
    
    @FXML
    protected void buttonClicked() {
        System.out.println("The button was clicked!");
    }
}

模块信息.java:

module iconbutton {
    requires javafx.controls;
    requires javafx.fxml;
    opens iconbutton to javafx.fxml;
    exports iconbutton;
}

IconedButton.fxml:

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

<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.image.ImageView?>

<fx:root prefHeight="106.0" prefWidth="364.0" type="AnchorPane" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <javafx.scene.layout.StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
          <children>
            <ImageView fx:id="icon" fitHeight="30.0" fitWidth="30.0" pickOnBounds="true" preserveRatio="true" StackPane.alignment="TOP_RIGHT" />
          </children>
      </javafx.scene.layout.StackPane>
       <Button fx:id="button" onAction="#buttonClicked" mnemonicParsing="false" prefWidth="285.0" text="Button" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="15.0" />
   </children>
</fx:root>

EDIT2:我除了睡觉什么都没做,问题就自行解决了。 如果您遇到同样的问题,只需重新启动并祈祷我会说!

问题自行解决。 重新启动你的电脑,做点别的事,然后再回来是我最好的建议!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM