如何在Swing中自动调整窗口大小

Nig*_*mas 4 java swing image jframe

我需要在JFrame中显示图像.如何根据图像的大小自动调整窗口大小.

mKo*_*bel 5

1.in你把图像视情况Icon / ImageIconJLabel

  • 要测试MaximumSizeJFrame是返回工具包混凝土显示器

  • 如果PreferedSize低于MaximumSize大小,则调用JFrame#pack()

  • 否则不得不打电话 setSize()

2.in的情况下,你把图像Icon / ImageIcon利用Custom PaintingJComponent,JPanel,JLabelEI然后

  • 然后这JComponen必须返回PreferredSize

    a)JFrame#pack()如果PreferedSize低于MaximumSize,则呼叫,

    b)否则必须打电话 JFrame#setSize()

    c)假设你不使用 Image#getScalledInstance

3.我将使用Icon,JLabel只有一个问题,图像可以小于屏幕上预期的尺寸,但没有问题,很可能将图像居中JLabel.CENTERJLabel

  • 还要考虑`JScrollPane`,显示在这里[http://stackoverflow.com/a/5129757/230513]. (4认同)
  • 示例****点2**使用***自定义绘画***显示[这里](http://stackoverflow.com/a/11372350/1057230) (3认同)