关于Swing的几种窗体布局管理

第一种为流式布局管理(FlowLayout),特点:组件按照加入的先后顺序从左到右对齐,一行满了就跳第二行。代码码上:

import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗体布局管理 流式布局
 * 
 * @author wxb
 *
 */
public class FlowLayoutDay {

	public static void main(String[] args) {
		FLayout gridLayout = new FLayout();
	}

}

class FLayout extends Frame {

	private static final long serialVersionUID = 1L;
	public final int WIDTH = 500;// 宽度
	public final int HEIGHT = 300;// 高度
	private FlowLayout fLayout;
	private Button btn1;// 按钮
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;

	public FLayout() {
		init();
	}

	private void init() {
		// 设置窗体大小
		this.setSize(WIDTH, HEIGHT);

		// 设置窗体的位置
		this.setLocationRelativeTo(null);

		// 初始化组件
		btn1 = new Button("Button1");
		btn2 = new Button("Button2");
		btn3 = new Button("Button3");
		btn4 = new Button("Button4");
		btn5 = new Button("Button5");
		btn1.setBackground(Color.red);// 设置颜色
		fLayout = new FlowLayout();
		this.setLayout(fLayout);// 设置窗体布局
		// 将按钮添加到窗体中,设置边框布局的位置
		this.add(btn1);
		this.add(btn2);
		this.add(btn3);
		this.add(btn4);
		this.add(btn5);

		// 设置窗体显示状态
		this.setVisible(true);
		/**
		 * 给窗体注册一个监听器 用适配器模式 这里采用匿名对象注册,也可以在外部定义一个监听器类,再调用
		 */
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

}

第二种为边框布局(BorderLayout,默认布局),特点:容器划分为东、南、西、北、中五个区域,每个区域放一个组件

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * 窗体布局管理 边框布局(东西南北中)
 * 
 * @author wxb
 *
 */
public class BorderLayoutDay {

	public static void main(String[] args) {
		BLayout gridLayout = new BLayout();
	}

}

class BLayout extends Frame {

	private static final long serialVersionUID = 1L;
	public final int WIDTH = 500;// 宽度
	public final int HEIGHT = 300;// 高度
	private Button btn1;// 按钮
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;
	private BorderLayout blLayout;

	public BLayout() {
		init();
	}

	private void init() {
		// 设置窗体大小
		this.setSize(WIDTH, HEIGHT);

		// 设置窗体的位置
		this.setLocationRelativeTo(null);

		// 初始化组件
		btn1 = new Button("Button1");
		btn2 = new Button("Button2");
		btn3 = new Button("Button3");
		btn4 = new Button("Button4");
		btn5 = new Button("Button5");
		btn1.setBackground(Color.red);// 设置颜色
		blLayout = new BorderLayout();
		this.setLayout(blLayout);
		// 将按钮添加到窗体中,设置边框布局的位置
		this.add(btn1, BorderLayout.NORTH);
		this.add(btn2, BorderLayout.SOUTH);
		this.add(btn3, BorderLayout.CENTER);
		this.add(btn4, BorderLayout.WEST);
		this.add(btn5, BorderLayout.EAST);

		// 设置窗体显示状态
		this.setVisible(true);
		// 注册一个监听事件 采用匿名对象,这种方法不好用,重写了一些其他没有用到的方法
		this.addWindowListener(new WindowListener() {

			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);

			}

			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		});
	}

}

第三种为网格布局(GridLayout),特点:容器划分为一个m*n的一个网格区域,每个区域放一个组件

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗体布局管理 网格布局
 * 
 * @author wxb
 *
 */
public class GridLayoutDay {

	public static void main(String[] args) {
		GLayout gridLayout = new GLayout();
	}

}

class GLayout extends Frame {

	private static final long serialVersionUID = 1L;
	public final int WIDTH = 500;// 宽度
	public final int HEIGHT = 300;// 高度
	private Button btn1;// 按钮
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;
	private GridLayout gridLayout;

	public GLayout() {
		init();
	}

	private void init() {
		// 设置窗体大小
		this.setSize(WIDTH, HEIGHT);

		// 设置窗体的位置
		this.setLocationRelativeTo(null);

		// 初始化组件
		btn1 = new Button("Button1");
		btn2 = new Button("Button2");
		btn3 = new Button("Button3");
		btn4 = new Button("Button4");
		btn5 = new Button("Button5");
		btn1.setBackground(Color.red);// 设置颜色
		gridLayout = new GridLayout(3, 2, 10, 10);
		this.setLayout(gridLayout);// 绝对布局
		// 将按钮添加到窗体中
		this.add(btn1);
		this.add(btn2);
		this.add(btn3);
		this.add(btn4);
		this.add(btn5);

		// 设置窗体显示状态为show
		this.setVisible(true);
		// 注册一个监听事件  采用适配器模式
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

}

第四种为绝对布局(AbsoluteLayout),特点:容器中的组件在添加是需要设置坐标

import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗体布局管理 绝对布局 this.setLayout(null); 添加控件时,需要设置坐标
 * 
 * @author wxb
 *
 */
public class AbsoluteLayoutDay {

	public static void main(String[] args) {
		ALayout gridLayout = new ALayout();
	}

}

class ALayout extends Frame {

	private static final long serialVersionUID = 1L;
	public final int WIDTH = 500;// 宽度
	public final int HEIGHT = 300;// 高度
	private Button btn1;// 按钮
	private Button btn2;

	public ALayout() {
		init();
	}

	private void init() {
		// 设置窗体大小
		this.setSize(WIDTH, HEIGHT);

		// 设置窗体的位置
		this.setLocationRelativeTo(null);

		// 设置窗体布局
		this.setLayout(null);// null代表绝对布局

		// 初始化组件
		btn1 = new Button("Button1");
		btn1.setBounds(10, 30, 100, 30);//设置坐标
		btn2 = new Button("Button2");
		btn2.setBounds(WIDTH - 110, HEIGHT - 45, 100, 35);
		btn1.setBackground(Color.red);// 设置颜色

		// 将按钮添加到窗体中,设置边框布局的位置
		this.add(btn1);
		this.add(btn2);

		// 设置窗体显示状态
		this.setVisible(true);
		/**
		 * 给窗体注册一个监听器 用适配器模式
		 */
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

}

第五种为卡片式布局(CardLayout),特点:容器中的组件每次只能显示一个(最后添加的那个)。

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 窗口布局管理器 卡片式布局
 * 实现可以前后翻页
 * @author wxb
 *
 */
public class CardLayoutDay {

	public static void main(String[] args) {
		CardFrame cardFrame = new CardFrame();
	}

}

class CardFrame extends Frame {

	private static final long serialVersionUID = -6562018127971835911L;
	public final int WIDTH = 500;// 宽度
	public final int HEIGHT = 300;// 宽度
	private Panel topPanel;// 上面的面板
	private Panel bottomPanel;// 下面的面板
	private Label lab1;// 文本标签
	private Label lab2;
	private Label lab3;
	private Label lab4;
	private Label lab5;
	// 按钮
	private Button firstBtn;// 第一个
	private Button previousBtn;// 前一个
	private Button nextBtn;// 下一个
	private Button lastBtn;// 最后一个
	private CardLayout cardLayout;// 卡片式布局
	private FlowLayout flowLayout;// 流式布局

	public CardFrame() {
		init();
	}

	private void init() {
		// 设置窗体大小;
		this.setSize(WIDTH, HEIGHT);
		// 默认为BorderLayout;

		// 初始化组件;
		topPanel = new Panel();
		bottomPanel = new Panel();
		lab1 = new Label("lab1", Label.CENTER);
		lab2 = new Label("lab2", Label.CENTER);
		lab3 = new Label("lab3", Label.CENTER);
		lab4 = new Label("lab4", Label.CENTER);
		lab5 = new Label("lab5", Label.CENTER);
		lab1.setFont(new Font("宋体", Font.BOLD, 36));
		lab2.setFont(new Font("宋体", Font.BOLD, 36));
		lab3.setFont(new Font("宋体", Font.BOLD, 36));
		lab4.setFont(new Font("宋体", Font.BOLD, 36));
		lab5.setFont(new Font("宋体", Font.BOLD, 36));
		firstBtn = new Button("first");
		previousBtn = new Button("previous");
		nextBtn = new Button("next");
		lastBtn = new Button("last");

		// 注册监听器
		firstBtn.addMouseListener(new MListener1());
		previousBtn.addMouseListener(new MListener1());
		nextBtn.addMouseListener(new MListener1());
		lastBtn.addMouseListener(new MListener1());

		// 指定topPanel为卡片式布局管理
		cardLayout = new CardLayout();
		topPanel.setLayout(cardLayout);

		// 指定bottomPanel为布局管理
		flowLayout = new FlowLayout();
		bottomPanel.setLayout(flowLayout);

		// 将label添加到topPanel容器中
		topPanel.add(lab1);
		topPanel.add(lab2);
		topPanel.add(lab3);
		topPanel.add(lab4);
		topPanel.add(lab5);
		topPanel.setBackground(Color.gray);

		// 将button添加到bottomPanel;
		bottomPanel.add(firstBtn);
		bottomPanel.add(previousBtn);
		bottomPanel.add(nextBtn);
		bottomPanel.add(lastBtn);
		bottomPanel.setBackground(Color.lightGray);

		// 设置topPanel和bottomPanel的位置
		this.add(topPanel, BorderLayout.CENTER);
		this.add(bottomPanel, BorderLayout.SOUTH);

		// 设置窗体居中;
		this.setLocationRelativeTo(null);

		// 显示窗体;
		this.setVisible(true);

		/**
		 * 给窗体注册一个监听器 用适配器模式
		 */
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	class MListener1 implements MouseListener {
		// 单击后;
		@Override
		public void mouseClicked(java.awt.event.MouseEvent e) {
			// System.out.println("单击鼠标后");
		}

		// 按下鼠标 不弹起
		@Override
		public void mousePressed(java.awt.event.MouseEvent e) {
			// System.out.println("按下鼠标不松");
		}

		// 按下并释放鼠标(单击)
		@Override
		public void mouseReleased(java.awt.event.MouseEvent e) {
			// System.out.println("按下并释放鼠标(单击)");
			Button obj = (Button) e.getSource();// 拿到事件源
			String btnText = obj.getLabel();// 拿到Label的内容
			if (btnText.equalsIgnoreCase("first")) { // 显示第一个;
				cardLayout.first(topPanel);
			} else if (btnText.equalsIgnoreCase("previous")) {
				cardLayout.previous(topPanel);
			} else if (btnText.equalsIgnoreCase("next")) {
				cardLayout.next(topPanel);
			} else if (btnText.equalsIgnoreCase("last")) {
				cardLayout.last(topPanel);
			}
		}

		@Override
		public void mouseEntered(java.awt.event.MouseEvent e) {
			// System.out.println("鼠标移到按钮上");

		}

		@Override
		public void mouseExited(java.awt.event.MouseEvent e) {
			// System.out.println("鼠标移出");
		}

	}
}


猜你喜欢

转载自blog.csdn.net/wxb52112181314/article/details/80267886
今日推荐