gpt4 book ai didi

java - Java程序输出可以显示图像吗?

转载 作者:太空宇宙 更新时间:2023-11-04 14:53:49 25 4
gpt4 key购买 nike

假设我有一个图像文件,其中显示了一只狗(仅作为示例)。在执行过程中,我想显示位于“C:\image.png”(再次强调)的图像,以便使用 System.out.println 或使用 Swing 类的基本 GUI 显示。我很想知道这一点,所以如果可能的话,如果你能解释一下的话,那就太有帮助了。如果不可能的话也一样。万分感谢!

编辑:是否不可能在 System.out.println 中包含图像

最佳答案

这应该适用于狗的图像和猫的图像。

import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;


public class ShowImage
{
public static void main(String[] args)
{
showImage("C:/image.png");
}

private static void showImage(final String fileName)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.getContentPane().setLayout(new GridLayout(1,1));
f.getContentPane().add(new JLabel(new ImageIcon(fileName)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}

对于马的图像,可能需要稍微调整。

关于java - Java程序输出可以显示图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23444478/

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