JavaFX:使用 fxml 添加应用程序图标

标签 java javafx fxml

我想向我的程序添加应用程序图标。我尝试按照以下方式执行此操作,但没有成功:

primaryStage.getIcons().add(new Image(Main.class.getResourceAsStream("lock.png")));
    primaryStage.getIcons().add(new Image(Controller.class.getResourceAsStream("lock.png")));
    final Parent root = FXMLLoader.load(getClass().getResource("passwordGenerator.fxml"));

primaryStage.getIcons().add(new Image("lock.png"));

我还尝试在没有 FXML 文件的情况下执行此操作,并且成功了。

如何使用 FXML 文件添加应用程序图标?

最佳答案

这是适合我的解决方案: 主类:

public class JavaFXIcons extends Application {
@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);
    stage.getIcons().add(new Image(JavaFXIcons.class.getResourceAsStream("stackoverflow.jpg"))) ;
    stage.setScene(scene);
    stage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
  }
}

FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxicons.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

关于JavaFX:使用 fxml 添加应用程序图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46006633/

相关文章:

java - IntelliJ IDEA 报契约违背警告

java - !MESSAGE 引用的部分尚不存在 : org. eclipse.jdt.ui.PackageExplorer

java - 在 JavaFX 中使用 CSS 时遇到问题

css - 使用 CSS 在 FXML 中设置 TableColumn 宽度的问题

java - CUCM 8.5 AXLAPI 中的示例 doAuthenticateUser 请求消息

java - 有没有办法在 Apache Wicket 中加载多个应用程序属性文件?

css - 从 JavaFX 中删除所有焦点边框

java - 对 -fx-alignment : LEFT saying no enum constant javafx. geometry.Pos.LEFT 发出警告

java - JavaFX Controller 中的 NullPointerException

JavaFX FXML 不响应文本输入或单击(事件)