首页javaimageJava Graphics - 如何将转换为PNG格式图像显示在对话框中

Java Graphics - 如何将转换为PNG格式图像显示在对话框中

我们想知道如何将转换为PNG格式图像显示在对话框中。
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

public class Main {

  public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.w3cschool.cn/style/download.png");
    BufferedImage origImg = ImageIO.read(url);

    JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(origImg)));

    File newFile = new File("new.png");
    ImageIO.write(origImg, "png", newFile);
    BufferedImage newImg = ImageIO.read(newFile);

    JOptionPane.showMessageDialog(null, new JLabel("New",
        new ImageIcon(newImg), SwingConstants.LEFT));
  }
}