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布局管理器

  • 相关阅读:
    Best Time to Buy and Sell Stock I II III
    数据挖掘算法面试题
    C# 从CIL代码了解委托,匿名方法,Lambda 表达式和闭包本质
    ASP.NET MVC 5
    net破解一(反编译,反混淆-剥壳,工具推荐)
    面试题及相关参考答案
    Linux 查看内核版本命令的相关说明
    c# 获取应用程序exe文件路径及退出应用程序的几种方法
    C# WebBrowser设置代理
    c# combobox控件的使用
  • 原文地址:https://www.cnblogs.com/cyg-06/p/5992194.html
Copyright © 2011-2022 走看看