首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

显示图片用什么控件比较好,如何显示

2012-07-24 
显示图片用什么控件比较好,怎么显示?如题[解决办法]JLabel:JLabel lab_hmnew JLable()lab_hm.setOpaque(

显示图片用什么控件比较好,怎么显示?
如题

[解决办法]
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好点

热点排行