java - JFX 图像未显示在网格 Pane 中

标签 java javafx

这是我的代码。编译时,网格 Pane 中仅显示按钮和标签,但图像不会显示。我将图像放入资源文件中,认为这可以解决问题,但仍然不会显示。

公共(public)类Main扩展应用程序{

@Override
public void start(Stage primaryStage) {
    //image controls
    Image image = new Image("file:resource/HotAirBalloon.jpg");
    ImageView imageView = new ImageView(image);

    //Label controls

    Label label1 = new Label ("Label 1");
    Label label2 = new Label ("Label 2");
    Label label3 = new Label ("Label 3");

    //button controls

    Button button1 = new Button("Button1");
    Button button2 = new Button("Button2");
    Button button3 = new Button("Button3");

    VBox vbox = new VBox(10 ,imageView);

    GridPane gridpane = new GridPane();

    gridpane.add(button1, 0,0);
    gridpane.add(label1,0,1);

    gridpane.add(button2, 0,2);
    gridpane.add(label2, 0, 3);

    gridpane.add(button3, 0,4);
    gridpane.add(label3, 0,5);
    gridpane.add(vbox, 1,0);



    gridpane.setHgap(10);
    gridpane.setPadding(new Insets(20));





    Scene scene = new Scene(gridpane);


    primaryStage.setScene(scene);

    primaryStage.show();
}


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

}

最佳答案

我已按原样复制了您的代码,它对我有用,将图像直接保存在我的 src 中并删除了路径的“文件:”部分,如下所示:

import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends javafx.application.Application {

    @Override
    public void start(Stage primaryStage) {
        //image controls
        Image image = new Image("2.png");
        ImageView imageView = new ImageView(image);

        //Label controls

        Label label1 = new Label ("Label 1");
        Label label2 = new Label ("Label 2");
        Label label3 = new Label ("Label 3");

        //button controls

        Button button1 = new Button("Button1");
        Button button2 = new Button("Button2");
        Button button3 = new Button("Button3");

        VBox vbox = new VBox(10 ,imageView);

        GridPane gridpane = new GridPane();

        gridpane.add(button1, 0,0);
        gridpane.add(label1,0,1);

        gridpane.add(button2, 0,2);
        gridpane.add(label2, 0, 3);

        gridpane.add(button3, 0,4);
        gridpane.add(label3, 0,5);
        gridpane.add(vbox, 1,0);



        gridpane.setHgap(10);
        gridpane.setPadding(new Insets(20));





        Scene scene = new Scene(gridpane);


        primaryStage.setScene(scene);

        primaryStage.show();
    }


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

}

screenshot !!

关于java - JFX 图像未显示在网格 Pane 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58610063/

相关文章:

java - JavaFX 应用程序中的随机 ArrayIndexOutOfBoundsException

java - 防止 opencsv 将引号写入 .csv 文件

java - 保存在 ArrayLists 中,但错误的输出写入文件

java - 统计 ArrayList 中一定范围内的数字

java - 为什么将 TextField 绑定(bind)到正在另一个线程上更新的属性最终会导致应用程序抛出错误?

针对 Java 8 构建的 JavaFX 应用程序 - 如何在 Java 11 上保持运行?

Java - 在给定值的情况下查找数组的元素?

Java:如何使用父类(super class)设计集合

Javafx进度条

java - 修复 java.lang.module.ResolutionException : two modules export the same package to another module in a non-modular app