gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:18:33 26 4
gpt4 key购买 nike

这是我的代码。编译时,网格 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com