setPreferredSize 的副作用是什么? [英] What are side effects of setPreferredSize?

查看:48
本文介绍了setPreferredSize 的副作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含多个面板的窗口.我无权访问窗口代码.(我只能修改面板的代码.)

I have window containing multiple panels. I don't have access to window code. (I can modify only panel’s code.)

我从面板上移除了几个组件.窗口缩小了它的大小.但是窗口太小,无法正确显示所有内容.

I removed few components from panel. Window has shrunk its size. But window is too small to display everything correctly.

我添加了行 setPreferredSize(getPreferredSize());.现在窗口大小合适.

I added line setPreferredSize(getPreferredSize());. Now window have right size.

setPreferredSize 的副作用是什么?

What are side effects of setPreferredSize?

使用 BorderLayout.应该忽略 getXXXSize().我的面板在中心.不适合屏幕的面板在北.

BorderLayout is used. Which should ignore getXXXSize(). My panel is in CENTRE. Panel which doesn't fit the screen is on NORTH.

推荐答案

这是正在发生的事情:

  • getPreferredSize() 查看之前是否设置了大小.如果没有,该方法会向组件本身(即您的 JPanel)的 LayoutManager 询问首选大小,然后根据组件计算出该大小.
  • setPreferredSize(...) 然后在 JPanel 上设置这个值,以备后用.
  • 稍后删除 JPanel 的一些组件.
  • 甚至稍后,当窗口尝试重新布局自己(或被告知这样做)时,窗口(或内容窗格/RootPane/...)的布局管理器会调用您的 JPanel 的 getPreferredSize()再次方法.
  • 现在 getPreferredSize() 不询问 JPanel 的 LayoutManager,而是简单地返回先前由 setPreferredSize() 设置的存储大小.
  • getPreferredSize() looks whether the size was set before. If not, the method asks the LayoutManager of the component itself (which is your JPanel) about the preferred size, which is then calculated from the components.
  • setPreferredSize(...) then sets this value on the JPanel, memorizing it for later.
  • Later you remove some components of the JPanel.
  • Even later, when the window tries to re-layout itself (or is told to do so), the window's (or contentpane's/RootPane's/...) Layoutmanager calls your JPanel's getPreferredSize() method again.
  • now getPreferredSize() does not ask the JPanel's LayoutManager, but simply returns the stored size previously set by setPreferredSize().

对于宽度,BorderLayout 忽略了 NORTH 和 South 组件的首选宽度,它只考虑了 CENTER、EAST 和 WEST.(高度类似).

我只是看了一下BorderLayout.preferredLayoutSize的实现(在Sun的1.6.0_13中),它是这样工作的:

I just took a look at the implementation of BorderLayout.preferredLayoutSize (in 1.6.0_13 from Sun), and it works like this:

宽度计算为

max(  EAST.width + CENTER.width + WEST.width + h-gaps,
      NORTH.width, SOUTH.width ) + insets

高度计算为

max( EAST.height, CENTER.height, WEST.height)
+ NORTH.height + SOUTH.height + v-gaps + insets

(每个width/height 都是这些组件的preferredSize 的值.)如果五个组件中的某些缺失,则不包括它们的高度/宽度,也不包括间隙.)

(Each of the width/height are the values of the preferredSize of these components.) If some of the five components are missing, their height/width is not included, neither are the gaps.)

它对 minimalLayoutSize 的作用相同,而 maximumLayoutSize 只返回 Integer.MAX_VALUE.

It works the same for minimalLayoutSize, while maximumLayoutSize simply returns Integer.MAX_VALUE.

所以,原则上它应该是开箱即用的.

So, in principle it should work out of the box.

但一般来说,如果窗口的布局不在你的控制之下,你就不必担心不在你控制之下的组件被截断:-)

But in general, if the layout of the window is not under your control, you should not have to worry about components not under your control being cut off :-)

这篇关于setPreferredSize 的副作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆