java - 如何以新形式导入图像

标签 java eclipse javafx

我是 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);
    }

}

最佳答案

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

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

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

关于java - 如何以新形式导入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43001804/

相关文章:

java - 当 Android 应用程序在设备上启动时进行调试

java - org.hamcrest.io.FileMatchers 是否包含在任何 hamcrest 构建中?

java - 快照整个TableView

java - 其类中的子类列表

java - Google App Engine 上的 Spring Security openid

eclipse - 在Eclipse中使用Subversive同时处理多个存储库

java - 未找到源类

java - Eclipse 3.7、 Ant 、Java 1.7

java - 如何初始化 TabPane (JavaFX)

javafx - 迭代 TreeView 节点