【问题标题】:BufferedImage on StackPane in JavaFXJavaFX 中 StackPane 上的 BufferedImage
【发布时间】:2015-10-13 22:18:04
【问题描述】:

我有一个 BufferedImage,我想在堆栈窗格中显示,因为我在 JavaFX 应用程序中工作。几天前我处于同样的情况,但我在 Java 中工作,在这种情况下,我确实喜欢这样:

public static  void VisImmagineDaPc(JFrame frame, BufferedImage image) throws Throwable
             {
                    if (label==null){
                    label = new JLabel(new ImageIcon(image));


                    frame.getContentPane().add(label, BorderLayout.AFTER_LAST_LINE);
                    // frame.setSize(10, 10);
                    int larghezza = frame.getWidth();
                    int altezza = frame.getHeight();

//                  frame.setSize(larghezza, altezza); 
                    frame.setSize(larghezza, altezza );
                    frame.setResizable(false);  
                    frame.setVisible(true);
        }

...

该方法继续使用其他代码,但目前并不重要。因此,在 Java 中,我使用图像创建了一个 Jlabel,然后添加到 Jframe。为了在堆栈窗格中显示图片,我必须在 JavaFX 中做什么?我试过content.getChildren().addAll(image); 其中content 是一个堆栈窗格,但它不起作用。

提前致谢,对于问题的简单性,我深表歉意。

【问题讨论】:

标签: java image swing javafx


【解决方案1】:

BufferedImage 转换为Javafx Image 后,您必须将Node 添加到StackPane 并且Image 不是Node,因此您可以使用Image 构造ImageView .

@FXML StackPane s;

@FXML void initialize(){

    BufferedImage b = ImageIO.read(file);
    Image i = SwingFXUtils.toFXImage(b, null);

    ImageView v = new ImageView(i);
    s.getChildren().add(v);
}

【讨论】:

  • 根据初始图像的来源,当然,您也许可以直接构造javafx.scene.image.Image,而不是先创建BufferedImage,然后再进行转换。
猜你喜欢
  • 2014-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
相关资源
最近更新 更多