zoukankan      html  css  js  c++  java
  • GUI Panel 容器以及布局管理器

    一、Panel是AWT中的另一个典型的容器,它代表不能独立存在、必须放在其他容器中使用。

    1、可作为容器来盛装其他组件,为放置组件提供空间。

    2、不能单独存在,必须放置到其他容器当中。

    3、默认使用FlowLayout作为布局管理器。

     1 public class PanelTest extends Frame
     2 {
     3     public static void main(String[] args)
     4     {
     5         Frame f = new Frame();
     6         Panel p = new Panel();
     7         p.add(new TextField(20));
     8         p.add(new Button("百度"));
     9         f.add(p);
    10         f.setBounds(50, 50, 300, 300);
    11         f.setVisible(true);
    12     }
    13 }

    二、FlowLayout布局管理器

    在FlowLayout布局管理器中,组件像流水一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列。

    三个构造器:

    FlowLayout:使用默认的对齐方式及默认的垂直间距、水平间距创建FlowLayout布局管理器。

    FlowLayout(int align):使用指定的对齐方式及默认的垂直间距、水平间距创建FlowLayout布局管理器。

    FlowLayout(int align,int hgap,int vgap):使用指定的对齐方式及指定的垂直间距、水平间距创建FlowLayout布局管理器。

     1 public class FlowLayoutTest
     2 {
     3     public static void main(String[] args)
     4     {
     5         Frame f = new Frame();
     6         f.setLayout(new FlowLayout(FlowLayout.LEFT,20,5));
     7         Button b1 = new Button("a1");
     8         Button b2 = new Button("a2");
     9         Button b3 = new Button("a3");
    10         f.add(b1);
    11         f.add(b2);
    12         f.add(b3);
    13         f.pack();
    14         f.setVisible(true);
    15         
    16     }
    17 }

    三、BorderLayout布局管理器

    BorderLayout将容器分为EAST(东),SOUTH(南),WEST(西),NORTH(北),CENTER(中)五个区域,普通组件可以放在这5个区域中的任意一个。

    构造器:

    BorderLayout():使用默认的水平间距、垂直间距创建BorderLayout布局管理器

    BorderLayout(int hgap,int vgap):使用指定的水平间距、垂直间距创建BorderLayout布局管理器。

     1 public class BorderLayoutTest extends Frame
     2 {
     3     public static void main(String[] args)
     4     {
     5         Frame f = new Frame();
     6         f.setLayout(new BorderLayout(30, 5));
     7         f.add(new Button("南"),BorderLayout.SOUTH);
     8         f.add(new Button("北"),BorderLayout.NORTH);
     9         f.add(new Button("中"));
    10         f.add(new Button("东"),BorderLayout.EAST);
    11         f.add(new Button("西"),BorderLayout.WEST);
    12         
    13         f.pack();
    14         f.setVisible(true);
    15     }
    16 }

    四、GridLayout布局管理器

  • 相关阅读:
    [JZOJ3339]【NOI2013模拟】wyl8899和法法塔的游戏
    [JZOJ3337] 【NOI2013模拟】wyl8899的TLE
    UVA 1262 Password
    UVA 10820 Send a Table
    UVA 12716 GCD XOR
    UVA 10791
    UVA 10375 choose and divide (唯一分解定理)
    欧拉函数
    51 Nod 1069 Nim游戏
    51 Nod 1242 矩阵快速幂求斐波那契数列
  • 原文地址:https://www.cnblogs.com/cyg-06/p/5992194.html
Copyright © 2011-2022 走看看