GUI布局:边界布局、流式布局、网格布局、卡片布局

边界布局


package guiTest;
//JFrame默认的是边界布局BorderLayout
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class BorderLayoutDemo {
	public static void main(String[] args) {
		JFrame f = new JFrame("边界布局BorderLayout");
		//JFrame的默认LayoutManager为BorderLayout
	//	f.setLayout(new BorderLayout());//可以不写,默认的就是流式布局
		JButton btn =  new JButton("北");
		f.add(btn,BorderLayout.NORTH);
		
		btn=new JButton("南");
		f.add(btn,BorderLayout.SOUTH);
		
		btn=new JButton("东");
		f.add(btn,BorderLayout.EAST);
		
		btn=new JButton("西");
		f.add(btn,BorderLayout.WEST);
		
		btn=new JButton("中");
		f.add(btn,BorderLayout.CENTER);
		
		f.pack();//也可以用f.setSize(222,222);来进行设置
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

流式布局



package guiTest;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class FlowLayoutDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JFrame f = new JFrame("流式布局FlowLayout");
		f.setLayout(new FlowLayout());
		for(int i=0;i<7;i++)
		{
			JButton btn=new JButton("Button"+i);
			f.add(btn);
		}
		f.setSize(300,250);
		//f.pack();默认边框设置宽度和长度刚刚好的样子
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

}


网格布局


<pre name="code" class="java">package guiTest;

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GirdLayoutDemo {

	public static void main(String[] args) {
		JFrame f = new JFrame("网格布局GirdLayout");
		//设置f的布局管理器为3行3列的GirdLayout组件间水平与垂直间距为5
		f.setLayout(new GridLayout(3,3,5,5));
		for(int i=1;i<10;++i)
		{
			JButton btn = new JButton(String.valueOf(i));
			f.add(btn);
		}
		f.pack();
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}

}

 

卡片布局


<pre name="code" class="java">package guiTest;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CardLayoutDemo {
	private static JPanel p;
	public static void main(String[] args) {
		JFrame f = new JFrame("卡片布局CardLayout");
		p=new JPanel();
		//设置p的布局管理器为卡片布局CardLayout
		p.setLayout(new CardLayout());
		
		//新建两个JPanel
		JPanel p1 = new JPanel();
		JPanel p2 = new JPanel();
		JLabel lb = new JLabel("第一个面板");
		p1.add(lb);//面板里面加标签
		lb=new JLabel("第二个面板");
		p2.add(lb);//面板里面加标签
		
		//将新建的两个JPanel面板添加到p中
		p.add(p1,"first");
		p.add(p2,"second");
		
		//设置默认显示first所对应的JPanel p1
		((CardLayout)p.getLayout()).show(p,"first");
		
		JButton btn = new JButton("改变面板");
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				//当点击'改变面板'时,显示second对应的JPanel p2
				((CardLayout)p.getLayout()).show(p,"second");
			}
		});
		f.add(btn,BorderLayout.NORTH);
		f.add(p,BorderLayout.CENTER);
		f.setSize(400,150);//f.pack();
		
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

 



  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

绝地反击T

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值