zoukankan      html  css  js  c++  java
  • JPanel

    JPanel就是一个容器,对一个容器一般只有四个操作:设定layout(排列方式),add component,remove component,get component。

    add(Componet,[int],[Object],[String]),其中int是component在panel中的index,默认index从0开始。Object和String表示一些有关layout的constraint信息,比如位置等。

    panels do not add colors to anything except their own background; however, you can easily add borders to them and otherwise customize their painting.

    Setting the Layout Manager

    a panel uses a layout manager to position and size its components. By default, a panel's layout manager is an instance of FlowLayout, which places the panel's contents in a row. You can easily make a panel use any other layout manager by invoking the setLayout method or by specifying a layout manager when creating the panel. The latter approach is preferable for performance reasons, since it avoids the unnecessary creation of a FlowLayout object.

    JPanel p = new JPanel(new BorderLayout()); //PREFERRED!
    

    This approach does not work with BoxLayout, since the BoxLayout constructor requires a pre-existing container. Here is an example that uses BoxLayout.

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));

    Adding Components

    When the layout manager is FlowLayout, BoxLayout, GridLayout, or SpringLayout, you will typically use the one-argument add method, like this:

    aFlowPanel.add(aComponent);
    aFlowPanel.add(anotherComponent);
    

    When the layout manager is BorderLayout, you need to provide an argument specifying the added component's position within the panel. For example:

    aBorderPanel.add(aComponent, BorderLayout.CENTER);
    aBorderPanel.add(anotherComponent, BorderLayout.PAGE_END);
    

    With GridBagLayout you can use either add method, but you must somehow specify grid bag constraints for each component.

    Reference: java api7

  • 相关阅读:
    思维导图 第八章 项目质量管理
    思维导图 第七章 项目成本管理
    redis安装与配置
    思维导图 第六章 项目进度管理
    思维导图 第五章 项目范围管理
    Linux下用户-组权限配置
    意灵魔法馆首页的初步设计
    try catch自定义异常类的使用
    使用freemarker时,生成的html出现乱码
    乱码问题
  • 原文地址:https://www.cnblogs.com/hf-cherish/p/3597072.html
Copyright © 2011-2022 走看看