關於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("鼠標移出");
		}

	}
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章