显示图片用什么控件比较好,怎么显示?
如题
------解决方案--------------------JLabel:
JLabel lab_hm=new JLable();
lab_hm.setOpaque(true);
lab_hm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tp/3.gif")));
JPanel:
import java.awt.Graphics;
import java.awt.Image;
import
java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
class tt extends JPanel {
private Image backgroundImage;
tt() {
URL url = null;
try {
url = new URL("http://www.yishujie.com/golo_pic/img/xinshang/picture/3837.jpg");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 从系统中加载图片
backgroundImage = new ImageIcon(url).getImage();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage != null) {
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
tt t = new tt();
f.add(t);
f.setBounds(0, 0, 500, 400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
我个人认为用JLabel好点