首页
登录 | 注册

图形用户界面按钮布局

package lsx;
import java.awt.*;
public class LayoutTest extends JFrame //建立类LayouTest继承于类JFrame
{
JButton btn1,btn2,btn3,btn4,btn5;
JPanel mainPan;
FlowLayout fLayout;
GridLayout gLayput;
BorderLayout blayout;
CardLayout cLayout;
public LayoutTest(String layoutstyle ) //定义构造函数
{
super(“布局方式”+layoutstyle);
mainPan=new JPanel();
fLayout=new FlowLayout(FlowLayout.LEFT);
GridLayout gLayout = new GridLayout(3,2);
blayout =new BorderLayout();
cLayout = new CardLayout(10,10);
if(layoutstyle ==”fLayout”)
mainPan.setLayout(fLayout);
else if(layoutstyle==”gLayout”)
mainPan.setLayout(gLayout);
else if(layoutstyle==”bLayout”)
mainPan.setLayout(blayout);
else if(layoutstyle==”cLayout”)
mainPan.setLayout(cLayout);
else
System.out.print(“布局方式输入错误!”);
getContentPane().add(mainPan);
btn1=new JButton(“按钮1”);
btn2=new JButton(“按钮2”);
btn3=new JButton(“按钮3”);
btn4=new JButton(“按钮4”);
btn5=new JButton(“按钮5”);
if(layoutstyle ==”fLayout”||layoutstyle == “gLayout”)
{
mainPan.add(btn1);
mainPan.add(btn2);
mainPan.add(btn3);
mainPan.add(btn4);
mainPan.add(btn5);
}
if(layoutstyle ==”bLayout”||layoutstyle == “cLayout”)
{
mainPan.add(btn1,”East”);
mainPan.add(btn2,”South”);
mainPan.add(btn3,”West”);
mainPan.add(btn4,”North”);
mainPan.add(BorderLayout.CENTER,btn5);
}
setSize(250,150); //设置界面的高度和宽度
setVisible(true); //设置界面的可见性
setDefaultCloseOperation(EXIT_ON_CLOSE); //设置窗口关闭时的默认操作
}
public static void main(String[] args)
{
// TODO 自动生成的方法存根
new LayoutTest(“fLayout”);
new LayoutTest(“gLayout”);
new LayoutTest(“bLayout”);
new LayoutTest(“cLayout”);
}
}

运行结果:
图形用户界面按钮布局



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号