美文网首页
用AWT编写用户界面二之布局管理器

用AWT编写用户界面二之布局管理器

作者: 编程人生 | 来源:发表于2020-11-15 11:35 被阅读0次

布局管理器 :为了实现跨平台的特效并且获得动态的布局结果,java 将容器内的所有组件安排给一个“布局管理器”负责管理。如:排列顺序,组件的大小,位置,当窗口移动或调整大小后组件如何变化等功能授权给对应的容器布局管理器来管理,不同的布局管理器使用不同的算法和策略,容器可以通过选择不同的布局管理器来决定布局

布局管理器的相关类主要包括:FlowLayout,BorderLayout,GridLayout,CardLayout,GridBagLayout。

/*

* TowButtons.java

*

* Created on 2020年11月14日, 上午11:16

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package mypkg;

import java.awt.*;

import java.awt.event.*;

public class TowButtons {

    private Frame f;  //窗口

    private Button b1;  //按钮1 

    private Button b2;  //按钮2

    /** Creates a new instance of TowButtons */

   public static void main(String [] args){

        TowButtons tb = new TowButtons();

        tb.go();

    }

    public void go(){

        f = new Frame("FlowLayout");

        f.setLayout(new FlowLayout());

        b1 = new Button("Press Me");

        b2 = new Button("Dont't Press me");

        f.add(b1);

        f.add(b2);

        f.pack(); //紧凑排列,其作用相当于setSize(),即让窗口尽量小。

                  // 小到刚刚能够包容住b1 b2 两个按钮。

        f.setVisible(true);

       f.addWindowListener(new WindowAdapter(){

            @Override

            public void windowClosing(WindowEvent e){

                      System.exit(0);

            }

        }); 

    }

}

1.FlowLayout 是Panel 和Applet 的默认布局管理器。 组件在容器的放置规律是从上到下,从左到右进行放置,如果容器足够宽, 第一个组件先添加到容器的第一行的最左边。后续的组件依次添加到上一个组件的右边,如果当前行已放置不下该组件,则放置到下一行的最左边。

FlowLayout 的构造方法主要有下面几种:

FlowLayout(FlowLayout,RIGHT , 20,40); 

第一个参数表示组件的对其方式,即在行中是居中,居左,居右还是居左对其,第二个参数是组件之间的横向间隔,第三个参数是组件之间的纵向间隔。单位是像素

FlowLayout(FlowLayout,LEFT);

居左对齐,横向间隔和纵向间隔都是默认值5个像素

FlowLayout();默认的对其方式是居中对齐,横向间隔和纵向间隔都是默认值5个像素。

/*

* TreeButtons.java

*

* Created on 2020年11月14日, 下午2:16

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package mypkg;

import java.awt.*;

import java.awt.event.*;

public class TreeButtons {

  public static void main (String []args){

        Frame f = new Frame();

        f.setLayout(new FlowLayout());

        Button button1 = new Button("OK");

        Button button2 = new Button("Open");

        Button button3 = new Button("Close");

        f.add(button1);

        f.add(button2);

        f.add(button3);

        f.setSize(300,100);

        f.setVisible(true);

 f.addWindowListener(new WindowAdapter(){

            @Override

            public void windowClosing(WindowEvent e){

                      System.exit(0);

            }

        });

  } 

}

2.BorderLayout 是window , Frame 和 Dialog 的默认的布局管理器。 BorderLayout 布局管理器把容器分为5个区域:North, Sorth ,East , West 和Center ,每个区域只能放置一个组件。 

如果容器采用BorderLayout 进行布局管理器, 在用add()方法添加组件的时候, 必须注明添加到哪个位置。在使用BorderLayout 的时候, 如果容器的大小发生变化, 其变化规律为:组件的相对位置不变,大小发生变化。

3.GridLayout 布局管理器。

GridLayout 布局管理器使容器中各个组件呈网格状布局,平均占据容器的空间。即使容器的大小发生变化,每个组件还是平均占据容器的空间。组件下往容器中放置位置的时候,是按照从上到下,从左到右的规律进行的。

4.CardLayout 布局管理器

CardLayout 布局管理器能够帮助用户处理两个以上甚至更多的成员共享同一个显示空间, 它把容器分成多层,每层的显示空间占据整个容器的大小, 但是每层之允许放置一个组件,当然每层都可以利用Panel 来实现复杂的用户界面。

/*

* TestCardLayout.java

*

* Created on 2020年11月15日, 上午9:52

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package mypkg;

import java.awt.*;

import java.awt.event.*;

public class TestCardLayout  implements ActionListener{

  private Panel p1,p2,p3;

  private Button b1,b2,b3;

  private Frame f;

  private CardLayout clayout = new CardLayout();  //定义CardLayout布局管理器

  public void create(){

    b1 = new Button("第一个");

    b2 = new Button("第二个");

    b3 = new Button("第三个");

    p1 = new Panel();

    p2 = new Panel();

    p3 = new Panel();

    f = new Frame("Test CardLayout");

    p1.add(b1);

    b1.addActionListener(this);//为按钮注册监听器

    p2.add(b2);

    b2.addActionListener(this);

    p3.add(b3);

    b3.addActionListener(this);

    f.setLayout(clayout);

    f.add(p1,"第一层");

    f.add(p1,"第二层");

    f.add(p1,"第三层");

    f.setSize(200,100);

    f.setVisible(true);

  }

  public static void main (String args []){

      TestCardLayout lc = new TestCardLayout();

      lc.create();

   }

  public void actionPerformed(ActionEvent e){

        clayout.next(f);

  } 

}

5.容器的嵌套

在复杂的图形用户界面设计中,为了使布局更加易于管理, 具有简洁的整体风格,一个包含了多个组件的容器本身也可以作为一个组件加到另一个容器中区,容器中再添加容器,这样就形成了容器的嵌套。

/*

* TestNesting.java

*

* Created on 2020年11月15日, 上午10:21

*

* To change this template, choose Tools | Template Manager

* and open the template in the editor.

*/

package mypkg;

import java.awt.*;

import java.awt.event.*;

/**

*

* @author yuwei

*/

public class TestNesting {

    public    Frame f;

    private Panel p;

    private Button bw, bc;

    private Button bfile , bhelp;

    private TextArea ta;

    public void creat(){

        f = new Frame("容器嵌套");

        ta  = new TextArea("test area");

        bw = new Button("West");

        bc = new Button("Work space region");

        f.add(bw,"West");

        f.add(bc,"Center");

        p  = new Panel();

        p.setLayout(new BorderLayout());

        f.add(bw,"West");

        f.add(bc,"Center");

        p = new Panel();

        p.setLayout(new BorderLayout());

        f.add(p,"North");

        bfile = new Button("文件");

        bhelp = new Button("帮助");

        p.add(bfile ,"North");

        p.add(bhelp,"West");

        p.add(ta,"Center");

        f.pack();

        f.setVisible(true);

    }

    public static void main(String args []){

            TestNesting tn  = new TestNesting();

            tn.creat();

    }

}

相关文章

网友评论

      本文标题:用AWT编写用户界面二之布局管理器

      本文链接:https://www.haomeiwen.com/subject/qflnbktx.html