【问题标题】:ImageView in Hbox won't show up in GridPaneHbox 中的 ImageView 不会显示在 GridPane 中
【发布时间】:2016-09-14 14:20:28
【问题描述】:

我正在制作一个显示汽车信息的应用程序,稍后我将添加 CRUD 功能,但现在我想在网格窗格上显示一张图片。

我有一个只包含 start 方法的小型 Main-Class,然后我有另一个包含所有 GUI 组件的类。这是图形用户界面:

GUI

我想让一个图像出现在屏幕的右上角。

我还有一个汽车类,我从中创建了汽车对象并将其放入一个数组中。该数组包含有关品牌、型号、reg.nr 和图像文件 URL 的信息。我还在 carclass 中创建了 get() 方法。

所以我成功地将所有其他组件添加到我的 GUIComponents 类中的网格中,最后我尝试添加这个,只是为了尝试显示第一个 carobjects 图像:

HBox hbCarImg = new HBox();
ImageView imageView = new ImageView(new Image(carArray[0].getImageURL()));
hbCarImg.getChildren().add(imageView);
add(hbCarImg, 2, 1, 2, 3);

我希望 hbox 出现在第 2 列第 1 行并跨越 2 列和 3 行。

当我这样做时没有任何反应。花了很多时间搜索这个,代码似乎很好,我不知道什么不起作用。

感谢任何提示或帮助!

【问题讨论】:

  • 您是否验证了图像 URL 正确并指向现有图像数据?
  • @VGR 是的,数组中的 URL 是有效的!
  • 通过给它一个可见的边框来验证 HBox 是否真的在你的 GridPane 中。例如,hbCarImg.setStyle("-fx-border-color: red;");
  • @VGR 我试过了,该框出现在屏幕上,正是我想要的,但仍然没有图像。 link
  • carArray[0].getImageURL()返回的字符串是什么?

标签: java javafx imageview gridpane


【解决方案1】:

虽然有点长,但应该这样做:

  //start with scrollpane

  ScrollPane sp = new ScrollPane();  

  //create a gridpane
  GridPane gb = new GridPane();
  gb.setHgap(5);
  gb.setVgap(5);


  //for debugging purposes, make sure you remove it
  gb.setGridLinesVisible(true);

  gb.setPadding(new Insets(10,10,10,10));
  gb.setPrefSize(600, 600);

  //initialize the number of rows and columns
  int imgCol = 0;
  int imgRow = 0;


  //i am making use of an entity bean class (POJO)
  //you can edit as you deem fit
  //create a list
  List<Info> list = rentalBn.carStatus();

  //create an String array list
  /**i stored the filepath of my images to the database 
  instead of theother way round**/
  ArrayList<String> fileArray = new ArrayList<>();
   ArrayList<String> nameArray = new ArrayList<>();
   ArrayList<String> modelArray = new ArrayList<>();
   ArrayList<String> manuArray = new ArrayList<>();
   ArrayList<Float> priceArray = new ArrayList<>();
   ArrayList<String> availArray = new ArrayList<>();


  ImageView img;

  //Use for loop to populate the arraylist with elements from the list
    for(Info inf : list){
      fileArray.add(inf.getImage());
      nameArray.add(inf.getCarname());
      modelArray.add(inf.getCarmodel());
      manuArray.add(inf.getManufacturer());
      priceArray.add(inf.getPrice());
      availArray.add(inf.getAvailable());
  }

System.out.println(fileArray.size());
System.out.println(nameArray.size());

for(int i = 0; i < fileArray.size(); i++){
     System.out.println(fileArray.get(i));
     System.out.println(nameArray.get(i));

//convert to bufferedImage
BufferedImage bi;
bi = ImageIO.read(new File(fileArray.get(i)));

//convert to FXImage
 Image image = SwingFXUtils.toFXImage(bi, null);


  img = new ImageView();
  img.setFitHeight(150);
  img.setFitHeight(150);
  img.setPreserveRatio(false);
  img.setImage(image);


 //add text below the images
 Text nameText = new Text();
 nameText.setText("Car Name: " + nameArray.get(i));

 Text modelText = new Text();
 modelText.setText("Car Model: " + modelArray.get(i));

 Text manuText = new Text();
 manuText.setText("Manufacturer: " + manuArray.get(i));

 Text priceText = new Text();
 priceText.setText("Price: " + priceArray.get(i));

 Text availText = new Text();
 availText.setText("Available: " + availArray.get(i));



 //create a VBox
  VBox hb = new VBox();
  hb.setPrefSize(150, 150);


  //add the imageView and other texts inside the box          
  hb.getChildren().addAll(img, nameText, modelText, 
  manuText, priceText, availText );                                       

 //create a pane, change the background
 Pane grid = new Pane();
 grid.setStyle("-fx-background-color: #00ff00;");
 grid.setPrefSize(200, 400);
 grid.getChildren().add(hb);

  //add the pane inside the gridpane
  gb.add(grid, imgCol, imgRow);

 //then add the gridpane inside the scrollpane
  sp.setContent(gb);

  imgCol++;

  //reset the column and row
  if(imgCol >3){
      imgCol =0;

      imgRow++;
  }

}


    Scene scene = new Scene(sp, 750, 700);


    primaryStage.setTitle("stage title");
    primaryStage.setFullScreen(false);

    primaryStage.setScene(scene);
    primaryStage.show();
}

POJO/entity 类可以参考THIS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多