如何實現窗口自動適應圖片調整尺寸。

chezone 2011-04-21 01:07:40
public class ExFrame extends JFrame{
ExFrame() throws IOException{
super("ExFrame");
Container content = getContentPane();
content.add(new ExPanel());
setVisible(true);

}
public static void main(String[] args) throws IOException
{
Image bi = ImageIO.read(new File("file.png"));
JFrame jf = new BigToSmall();
jf.setSize(bi.getWidth(null), bi.getHeight(null));
}
}
class ExPanel extends JPanel{
public BsPanel() throws IOException{
bi = ImageIO.read(new File("file.png"));
}
public void paint(Graphics g){
g.drawImage(bi, 0, 0, this);
System.out.println(bi.getWidth(this) + " " + bi.getHeight(this));
}
Image bi;
}



這樣只能牽強地根據圖片大小調整尺寸,因為標題欄也占了空間,所以並不能完美地顯示圖片,請問怎樣才能做到適應圖片大小自己調整尺寸呢,希望大家的幫助,謝謝。
...全文
98 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chezone 2011-04-21
  • 打赏
  • 举报
回复
感謝樓上的幫助。
huntor 2011-04-21
  • 打赏
  • 举报
回复
ExPanel 添加方法
// 图片大小 加上 inset
public Dimension getPreferredSize(){
return new Dimension(..,..);
}

content.add(new ExPanel());
pack(); // ++++

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧