簡體   English   中英

JavaFX:使用fxml添加Application-Icon

[英]JavaFX: Adding Application-Icon with 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM