简体   繁体   English

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

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

I'm new to stackoverflow so if I do some mistakes, don't hesitate and tell me!我是stackoverflow的新手,所以如果我犯了一些错误,请不要犹豫,告诉我!

I've been working on a personnal project recently and wanted to create a GUI with specific widgets.我最近一直在做一个个人项目,想创建一个带有特定小部件的 GUI。 I am working in Java using JavaFX with its GUI: scenebuilder.我正在使用 JavaFX 及其 GUI:scenebuilder 在 Java 中工作。 So far, I've been able to overcome all my issues by myself thanks to other questions asked here.到目前为止,由于这里提出的其他问题,我已经能够自己克服所有问题。 This one is about adding my own controller to my project using scenebuilder.这个是关于使用场景构建器将我自己的 controller 添加到我的项目中。 I have created my controller (a simple Button with an overlapping ImageView github link ) using the model found here .我已经创建了我的 controller(一个简单的按钮,带有重叠的 ImageView github 链接)使用找到的Z20F38Z35E630DAF39444链接。 As you can test, this sample app works fine.正如您可以测试的那样,此示例应用程序运行良好。 After that, I export as jar and try to add it in scenebuilder.之后,我导出为 jar 并尝试将其添加到场景构建器中。

Here is my issue: if I don't load (line 32 of IconedButton.java) I can see my control in scenebuilder (without any display but it appears on the list) but if i do load it it disappears and I can't see it on scenebuilder.这是我的问题:如果我不加载(IconedButton.java 的第 32 行),我可以在 scenebuilder 中看到我的控件(没有任何显示,但它出现在列表中)但是如果我加载它,它就会消失并且我不能在scenebuilder上看到它。 I assume my issue comes from my fxml file but it loads correctly outside scenebuilder and I have tried everything I have found about adding custom controllers in scenebuilder.我假设我的问题来自我的 fxml 文件,但它在 scenebuilder 之外正确加载,我已经尝试了我发现的关于在 scenebuilder 中添加自定义控制器的所有内容。 I'd like to use it after on another project and maybe adding it as a jar isn't the right way to do it but I try to do things step after step in order to understand everything.我想在另一个项目上使用它,也许将它添加为 jar 不是正确的方法,但我尝试一步一步地做事情以了解一切。

I hope I haven't done anything wrong with this post and believe someone out there can help me!我希望我在这篇文章中没有做错任何事情,并相信有人可以帮助我!

Find here my code: github link and here the tutorial I've followed在这里找到我的代码: github 链接这里是我遵循的教程

EDIT:编辑:

App.java: 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 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!");
    }
}

module-info.java:模块信息.java:

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

IconedButton.fxml: 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: I hava absolutely done nothing but sleep and the problem resolved itself. EDIT2:我除了睡觉什么都没做,问题就自行解决了。 If you encounter the same issue just restart and pray I'd say!如果您遇到同样的问题,只需重新启动并祈祷我会说!

Issue solved by itself.问题自行解决。 Restart your PC, do something else and come back later would be my best advice!重新启动你的电脑,做点别的事,然后再回来是我最好的建议!

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

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