【问题标题】:How to import images in new form如何以新形式导入图像
【发布时间】:2017-08-17 12:40:08
【问题描述】:

我是 JavaFX 新手,这是我的代码。此应用程序有一个按钮,如果单击该按钮会打开新表单。有谁知道如何在新表单中插入图片?

package javafxwindow;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

我使用的包。 JavaFXWindow 类。

public class JavaFXWindow extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Open a New Window");
        btn.setOnAction((ActionEvent event) -> {
            Label secondLabel = new Label("Hello");

            StackPane secondaryLayout = new StackPane();
            secondaryLayout.getChildren().add(secondLabel);

            Scene secondScene = new Scene(secondaryLayout, 200, 100);

            Stage secondStage = new Stage();
            secondStage.setTitle("New Stage");
            secondStage.setScene(secondScene);

            secondStage.show();

        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();

        primaryStage.setOnCloseRequest(e -> Platform.exit());

    }

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

}

【问题讨论】:

    标签: java eclipse javafx


    【解决方案1】:

    只需将子 ImageView 添加到 secondaryLayout。例如;

    ImageView imageViewTest = new ImageView(new Image("com/test/images/test.png"));
    secondaryLayout.getChildren().add(imageViewTest);
    

    但是您需要为 test.png 编写正确的路径。它会写在你的 src 包之后。

    【讨论】:

    • 如何以新形式添加4张图片?谢谢。
    • “路径”具有误导性。它应该是一个URL! @somiludak 自己尝试一下怎么样。警告:您可能会获得一些执行此操作的经验...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多