我是 stackoverflow 的新手,所以如果我犯了一些错误,请不要犹豫并告诉我!
我最近一直在从事一个个人项目,想要创建一个带有特定小部件的 GUI。我正在 Java 中使用 JavaFX 及其 GUI:scenebuilder。到目前为止,由于这里提出的其他问题,我已经能够自己解决所有问题。 这是关于使用 scenebuilder 将我自己的控制器添加到我的项目中。我使用here找到的模型创建了我的控制器(一个带有重叠 ImageView github 链接的简单按钮)。正如您可以测试的那样,这个示例应用程序运行良好。之后,我导出为 jar 并尝试将其添加到 scenebuilder 中。
这是我的问题:如果我不加载(IconedButton.java 的第 32 行),我可以在 scenebuilder 中看到我的控件(没有任何显示,但它出现在列表中),但如果我加载它,它就会消失,我可以'在场景构建器上看不到它。我假设我的问题来自我的 fxml 文件,但它在 scenebuilder 之外正确加载,并且我已经尝试了有关在 scenebuilder 中添加自定义控制器的所有内容。 我想在另一个项目上使用它,也许将它添加为 jar 不是正确的方法,但我尝试一步一步地做事情,以便理解一切。
我希望这篇文章没有做错任何事,并相信有人可以帮助我!
编辑:
应用程序.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:我除了睡觉什么也没做,问题就自行解决了。如果您遇到同样的问题,请重新启动并祈祷我会说!
问题自行解决。重新启动你的电脑,做点别的事情,然后再回来是我最好的建议!
我的问题是 Scenebuilder 在我的控制器类末尾添加了一个随机 {,我只是手动编辑了 fx 文件,之后工作正常。
为此,只需将 fx:controller="controller{" 替换为我的例子中的正确名称 fx:controller="controller"
您将在 fxml 文件的父级上找到 fx:controller
或者顶部,如果你想这样称呼它的话