![](/img/trans.png)
[英]Allow a Java Application User to import images to be displayed within the program
[英]Is it possible for images to be displayed in Java program output?
說我有一個圖像文件,其中顯示了一只狗(例如)。 在執行期間,我想顯示位於“ C:\\ image.png”處的圖像(再次說一遍),該圖像將通過使用Swing類使用System.out.println或基本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);
}
});
}
}
對於馬的圖像,可能需要稍作調整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.