将JPanel组件放置到JFrame中,我想设置JFrame大小适合图片的大小,怎么获取图片的大小阿????
先得到图片的大小,然后再设置JFrame的大小
[CODE]
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.*;
public class ImageTest
{
public static void main(String[] args)
{
ImageFrame frame=new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ImageFrame extends JFrame
{
public ImageFrame()
{
setTitle("Picture");
setSize( ); 这里怎么获取图片大小????
ImagePanel panel=new ImagePanel();
add(panel);
}
}
class ImagePanel extends JPanel
{
private Image image;
public ImagePanel()
{
try
{
image=ImageIO.read(new File("bubbles.jpg"));
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image==null)return;
g.drawImage(image, 0, 0,null);
}
}[/CODE]