JavaSwing图形界面编程之布局管理器(一)

时间:2023-01-26 16:49:52

代码运行效果:注意看空间水平之间和垂直之间的距离

JavaSwing图形界面编程之布局管理器(一)

package three.day.frame;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class BorderLayoutDemo01 
{
public static void main(String[] args)
{
Frame mf = new Frame("BorderLayoutDemo01");

mf.setLayout(new BorderLayout(20,10));

//定义五个按钮对象,按钮上文本分别为East,South,West,North,Center

Button bt1 = new Button("East");
Button bt2 = new Button("South");
Button bt3 = new Button("West");
Button bt4 = new Button("North");
Button bt5 = new Button("Center");

mf.setFont(new Font("宋体", Font.PLAIN, 14));//设置窗体使用的字体为宋体,普通文本,字体大小为14

//将按钮添加到Frame窗口

mf.add(bt1,"East");
mf.add(bt2,"South");
mf.add(bt3,"West");
mf.add(bt4,"North");
mf.add(bt5,"Center");
//设置关闭窗口同时终止应用程序
mf.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}

});

//设置窗口宽200,高200

mf.setSize(new Dimension(200,200));

//设置窗口初始化位置(窗口右上角的坐标为x=450,y=50)

mf.setLocation(new Point(450,50));

//显示窗口

mf.setVisible(true);
}


}




FlowLayout:

JavaSwing图形界面编程之布局管理器(一)


package three.day.frame;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class FlowLayoutDemo01 
{
public static void main(String[] args)
{
Frame mf = new Frame("FlowLayoutDemo01");
mf.setLayout(new FlowLayout(FlowLayout.LEFT ,10, 20));
Button bt1 = new Button("East");
Button bt2 = new Button("South");
Button bt3 = new Button("West");
Button bt4 = new Button("North");
Button bt5 = new Button("Center");
mf.setFont(new Font("宋体", Font.PLAIN, 14));
mf.add(bt1);
mf.add(bt2);
mf.add(bt3);
mf.add(bt4);
mf.add(bt5);

mf.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}

});
mf.setSize(new Dimension(350,100));
mf.setLocation(new Point(450,50));
mf.setVisible(true);
}


}




GridLayout:

JavaSwing图形界面编程之布局管理器(一)

package three.day.frame;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;








public class GridLayoutDemo01 
{
public static void main(String[] args)
{
Frame mf = new Frame("GridLayoutDemo01");
mf.setLayout(new GridLayout(3,2,10,10));//定义一个3行2列,控件间水平距离为10,垂直距离为10
Button bt1 = new Button("East");
Button bt2 = new Button("South");
Button bt3 = new Button("West");
Button bt4 = new Button("North");
Button bt5 = new Button("Center");
mf.setFont(new Font("宋体", Font.PLAIN, 14));

mf.add(bt1,"East");
mf.add(bt2,"South");
mf.add(bt3,"West");
mf.add(bt4,"North");
mf.add(bt5,"Center");

mf.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}

});
mf.setSize(new Dimension(200,200));
mf.setLocation(new Point(450,50));
mf.setVisible(true);
}


}



CardLayout:

JavaSwing图形界面编程之布局管理器(一)

其中,South,West,Center,North在East的下面,可通过事件监听器切换到下一张Card,这跟flash中的帧动画有异曲同工之妙


package three.day.frame;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

public class CardLayoutDemo01 
{
public static void main(String[] args)
{
Frame mf = new Frame("CardLayoutDemo01");
mf.setLayout(new CardLayout(10,20));
Button bt1 = new Button("East");
Button bt2 = new Button("South");
Button bt3 = new Button("West");
Button bt4 = new Button("North");
Button bt5 = new Button("Center");


mf.setFont(new Font("宋体", Font.PLAIN, 14));


mf.add(bt1,"1");
mf.add(bt2,"2");
mf.add(bt3,"3");
mf.add(bt4,"4");
mf.add(bt5,"5");

mf.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}

});
mf.setSize(new Dimension(200,200));
mf.setLocation(new Point(450,50));
mf.setVisible(true);
}


}


JavaSwing图形界面编程之布局管理器(一)

点击CardLayout的“男人”按钮,就切换到“女人按钮了

JavaSwing图形界面编程之布局管理器(一)



package three.day.frame;


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;




public class LayoutUsage extends Frame
{
private Panel pl01 = null;
private Panel pl02 = null;
private Panel pl03 = null;
private Panel pl04 = null;
    public LayoutUsage(String title)
    {
    this.setTitle(title);
    this.setSize(500,500);
    this.setLocation(new Point(200,100));
    this.addWindowListener(new WindowAdapter()
    {
public void windowClosing(WindowEvent e) 
    {
    System.exit(0);
    }
    });
    setBorderLayout();
    setFlowLayout();
    setCardLayout();
    setGridLayout();
    this.setLayout(new GridLayout(2,2));
    this.add(pl01);
    this.add(pl02);
    this.add(pl04);
    this.add(pl03);
   
   
    }
    public void setBorderLayout()
    {
    pl01 = new Panel();
    pl01.setLayout(new BorderLayout());
    Button bt1 = new Button("North");
    Button bt2 = new Button("South");
    Button bt3 = new Button("West");
    Button bt4 = new Button("East");
    Button bt5 = new Button("Center");
    pl01.add(bt1,BorderLayout.NORTH);
    pl01.add(bt2,BorderLayout.SOUTH);
    pl01.add(bt3,BorderLayout.WEST);
    pl01.add(bt4,BorderLayout.EAST);
    pl01.add(bt5,BorderLayout.CENTER);
   
    }
    public void setFlowLayout()
    {
    pl02 = new Panel();
    pl02.setLayout(new FlowLayout());
    Button bt1 = new Button("Flow01");
    Button bt2 = new Button("Flow02");
    Button bt3 = new Button("Flow03");
    Button bt4 = new Button("Flow04");
    Button bt5 = new Button("Flow05");
    pl02.add(bt1);
    pl02.add(bt2);
    pl02.add(bt3);
    pl02.add(bt4);
    pl02.add(bt5);
    }
    public void setCardLayout()
    {
    pl03 = new Panel();
    final CardLayout cl = new CardLayout();
    pl03.setLayout(cl);
    Button bt1 = new Button("男人");
    Button bt2 = new Button("女人");
    pl03.add(bt1,"1");
    pl03.add(bt2,"2");
    ActionListener al = new ActionListener()
    {
    int i = 0;
@Override
public void actionPerformed(ActionEvent e) {
cl.next(pl03);
System.out.println(++i);
}
   
    };
    bt1.addActionListener(al);
    bt2.addActionListener(al);
   
   
    }
    public void setGridLayout()
    {
    pl04 = new Panel();
    pl04.setLayout(new GridLayout(2,2));
    Button bt1 = new Button("网格1");
        bt1.addActionListener(new ActionListener()
        {


@Override
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
System.out.println(ae.getSource());
((Button) ae.getSource()).setLabel("Grid1");
}
       
        });
    Button bt2 = new Button("网格2");
    Button bt3 = new Button("网格3");
    Button bt4 = new Button("网格4");
    pl04.add(bt1);
    pl04.add(bt2);
    pl04.add(bt3);
    pl04.add(bt4);
   
    }

public static void main(String[] args) {
LayoutUsage lu = new LayoutUsage("http://www.baidu.com");
lu.setVisible(true);


}


}