What are the side effects of setPreferredSize?

I have a window containing several panels. I do not have access to window code. (I can only change the panel code.)

I removed several components from the panel. The window has reduced its size. But the window is too small to display everything correctly.

I have added a line setPreferredSize(getPreferredSize());. Now the window has the correct size.

What are the side effects of setPreferredSize?

Edit: using BorderLayout. Which should ignore getXXXSize (). My panel is in the CENTER. A panel that does not fit the screen is in the NORTH.

+3
source share
3 answers

This is what happens:

  • getPreferredSize() , . , LayoutManager ( JPanel) , .
  • setPreferredSize(...) JPanel, .
  • JPanel.
  • , ( ), Layoutmanager ( contentpane's/RootPane/...) JPanel getPreferredSize().
  • getPreferredSize() JPanel LayoutManager, , setPreferredSize().

BorderLayout , CENTER, EAST WEST. ( ).

BorderLayout.preferredLayoutSize ( 1.6.0_13 Sun), :

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 .) - , / , .)

minimalLayoutSize, maximumLayoutSize Integer.MAX_VALUE.

, .

, , , , , : -)

+3

, .

  • .

, , setPreferredSize() , ui . , ui pref . pref , ui pref.

. Jcomonent.getPreferredSize():

    if (isPreferredSizeSet()) {
        return super.getPreferredSize();
    }
    Dimension size = null;
    if (ui != null) {
        size = ui.getPreferredSize(this);
    }

layout-manager, () getPreferredSize() . , flowlayout pref,

+2

setPreferredSize() .

getPreferredSize() .

setPreferredSize (getPreferredSize()) , Paŭlo Ebermann.

0

Source: https://habr.com/ru/post/1794392/


All Articles