zoukankan      html  css  js  c++  java
  • setPreferredSize的用法

    The slightly longer answer is: use setSize() if your component's parent has no layout manager, and setPreferredSize() and its related setMinimumSize and setMaximumSize if it does.
    setSize() probably won't do anything if the component's parent is using a layout manager; the places this will typically have an effect would be on top-level components (JFrames and JWindows) and things that are inside of scrolled panes. You also must call setSize if you've got components inside a parent without a layout manager.
    As a general rule, setPreferredSize() should do the "right thing" if you've got a layout manager; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, and then using setSize() and setLocation() to position those components according to the layout's rules. So (as an example) a BorderLayout will try to make the bounds of its "north" region equal to the preferred size of its north component - they may end up larger or smaller than that, depending on the size of the frame, the size of the other components in the layout, and so on

    上面大概意思说的就是:

    1.setPreferredSize需要在使用布局管理器的时候使用,布局管理器会获取空间的preferredsize,因而可以生效。例如borderlayout在north中放入一个panel,panel的高度可以通过这样实现:panel.setPreferredSize(new Dimension(0, 100));这样就设置了一个高度为100的panel,宽度随窗口变化。

    2.setSize,setLocation,setBounds方法需要在不使用布局管理器的时候使用,也就是setLayout(null)的时候可以使用这三个方法控制布局。

  • 相关阅读:
    Qt类继承关系图
    回归Qt——写在Qt5.10发布之日
    Jdk1.7下的HashMap源码分析
    Jdk1.8下的HashMap源码分析
    八皇后||算法
    设计模式之一单例模式
    多线程之美8一 AbstractQueuedSynchronizer源码分析<二>
    多线程之美7一ReentrantReadWriteLock源码分析
    多线程之美6一CAS与自旋锁
    多线程之美5一 AbstractQueuedSynchronizer源码分析<一>
  • 原文地址:https://www.cnblogs.com/new-comer/p/5062484.html
Copyright © 2011-2022 走看看