62,635
社区成员




import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class SetIconTest extends JFrame
{
// 用一个JLabel放置图片
private JLabel label = new JLabel();
public JLabel getLabel()
{
return label;
}
public SetIconTest()
{
init();
}
private void init()
{
this.setTitle("看图程序");
this.setPreferredSize(new Dimension(800, 600));
this.getContentPane().add(new JScrollPane(label), BorderLayout.CENTER);
this.setVisible(true);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
//new SetIconTest().getLabel().setIcon(new ImageIcon("1.bmp"));
new SetIconTest().getLabel().setIcon(new ImageIcon("jiu.jpg"));
}
}
public ImageIcon(String filename, String description) {
image = Toolkit.getDefaultToolkit().getImage(filename);
if (image == null) {
return;
}
this.filename = filename;
this.description = description;
loadImage(image);
}
getImage
public abstract Image getImage(String filename)
Returns an image which gets pixel data from the specified file, whose format can be either GIF, JPEG or PNG. The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image.
Since the mechanism required to facilitate this sharing of Image objects may continue to hold onto images that are no longer in use for an indefinite period of time, developers are encouraged to implement their own caching of images by using the createImage variant wherever available. If the image data contained in the specified file changes, the Image object returned from this method may still contain stale information which was loaded from the file after a prior call. Previously loaded image data can be manually discarded by calling the flush method on the returned Image.
This method first checks if there is a security manager installed. If so, the method calls the security manager's checkRead method with the file specified to ensure that the access to the image is allowed.
Parameters:
filename - the name of a file containing pixel data in a recognized file format.
Returns:
an image which gets its pixel data from the specified file.
Throws:
SecurityException - if a security manager exists and its checkRead method doesn't allow the operation.
See Also:
createImage(java.lang.String)
所以问题就纠结在这里了,找到老根了。
现在改成这样了总算是可以显示了:
try
{
BufferedImage bi = ImageIO.read(new File("1.bmp"));
ImageProducer producer = bi.getSource();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.createImage(producer);
ImageIcon ic = new ImageIcon(image);
new SetIconTest().getLabel().setIcon(ic);
}
catch (IOException e)
{
e.printStackTrace();
}
结贴!!!!!!!!!(查阅的时候曾经翻到论坛2001年都有人问过这样问题也是没答案,纠结啊,还有非要什么双关注才能发私信请人帮忙,累啊!)java.io.File file=new java.io.File("jiu.bmp");
try{
java.awt.Image image = javax.imageio.ImageIO.read(file);
javax.swing.ImageIcon icon = new javax.swing.ImageIcon(image);
new SetIconTest().getLabel().setIcon(icon);
}catch(java.io.IOException e){}