Swing如何显示图片

1)使用JLabel支持html的特性显示图片

package test;

import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * 使用JLabel支持html显示图片
 * @author wasw100
 */
public class DisplayImageHtml extends JFrame {

 private JLabel lblImg;

 public DisplayImageHtml() {

  setTitle("swing显示图片");
  setSize(200, 140);
  setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  lblImg = new JLabel();
  add(lblImg);
  setVisible(true);

  lblImg.setText("<html><img src='http://www.wasw100.com/wp-content/themes/inove/img/logo.gif' /><html>");
 }

 public static void main(String[] args) {
  new DisplayImageHtml();
 }
}

2)使用JLabel的setIcon显示图片

package test;

import java.net.MalformedURLException;
import java.net.URL;

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

public class DisplayImageIcon extends JFrame {

 private JLabel lblImg;
 private Icon icon;

 public DisplayImageIcon() {

  setTitle("swing显示图片");
  setSize(200, 140);
  setDefaultCloseOperation(DISPOSE_ON_CLOSE);

  lblImg = new JLabel();
  add(lblImg);
  setVisible(true);

  try {
   icon = new ImageIcon(new URL("http://www.wasw100.com/wp-content/themes/inove/img/logo.gif"));
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
  lblImg.setIcon(icon);

 }

 public static void main(String[] args) {
  new DisplayImageIcon();
 }
}

你可能感兴趣的:(html,.net,swing)