Java Swing 布局管理器

package javagui.ui;

import java.awt.Color;
import java.awt.Image;

import javax.swing.ImageIcon;



public class GUIMain {

	public static void main(String[] args) {
		
		/*Window1 win1=new Window1();
		win1.setSize(400, 300);
		win1.Init();
		win1.setIconImage(null);
		
		String path= ClassLoader.getSystemResource("res/icon.png").getPath();
		
		Image img=null;
		
		ImageIcon ic=new ImageIcon(path);
		img= ic.getImage();
		
		win1.setIconImage(img);
		//win1.getContentPane().setBackground(Color.red);
		//win1.setBackground(Color.red);
		win1.Show();
		*/
		
		Window3 win3=new Window3();
		win3.Init();
		win3.setSize(400, 300);
		win3.Show();
		
		
	}

}

package javagui.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

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

/**
 * 流式布局
 * 组件在容器中按加入顺序逐行定位,行内从左到右,一行满后换行,默认对齐方式为居中对齐
 * @author dream
 *
 */
public class Window1 extends JFrame implements WindowListener,MouseListener {
	private JLabel label;
	private JButton button1;
	private JButton button2;
	private JButton button3;
	
	public void Init(){
		
		//按钮
		button1=new JButton();
		button1.setText("按钮1");
		button1.setSize(100, 50);
		
		//按钮
		button2=new JButton();
		button2.setText("按钮2");
		
		//按钮
		button3=new JButton();
		button3.setText("按钮3");
		
		
		this.getContentPane().setLayout(new FlowLayout());
		
		//添加组件
		this.getContentPane().add(button1);
		this.getContentPane().add(button2);
		
		JPanel jpanel=new JPanel(new FlowLayout(FlowLayout.LEFT,10,10));
		jpanel.setBackground(Color.red);
		
		this.getContentPane().add(jpanel);
		jpanel.setPreferredSize(new Dimension(200, 100));
		jpanel.add(button3);
		
		this.addWindowListener(this);
		this.setResizable(true);
	}
	
	public void Show(){
		this.setVisible(true);
	}

	
	/**
	 * 窗体激活时执行
	 */
	public void windowActivated(WindowEvent arg0) {
		System.out.println("windowActivated");
	}

	/**
	 * 窗体已关闭时执行
	 */
	public void windowClosed(WindowEvent arg0) {
		System.out.println("windowClosed");
	}

	/**
	 * 窗体关闭前执行
	 */
	public void windowClosing(WindowEvent arg0) {
		System.out.println("windowClosing");
		this.dispose();
		System.exit(0);
	}

	/**
	 * 窗体由激活转为未激活时执行(也就是不是当前焦点窗口)
	 */
	public void windowDeactivated(WindowEvent arg0) {
		System.out.println("windowDeactivated");
	}

	public void windowDeiconified(WindowEvent arg0) {
		System.out.println("windowDeiconified");
		
	}

	public void windowIconified(WindowEvent arg0) {
		System.out.println("windowIconified");
	}
	/**
	 * 窗体显示前执行
	 */
	public void windowOpened(WindowEvent arg0) {
		System.out.println("windowOpened");
	}

	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent arg0) {
		System.out.println("mouseEntered");
		
	}

	public void mouseExited(MouseEvent arg0) {
		System.out.println("mouseExited");
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}

package javagui.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;

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

/**
 * 边界布局
 * 是Window及子类(Frame,Dialog)的默认布局管理器。
组件布局效果:将整个容器分为:东,西,南,北,中(East,West,South,North,Center)五部分,
组件只能被添加到指定的区域,默认加的Center区域,每个区域只能加入一个组件。东,西为垂直缩放,南,北为水平缩放。
 * @author dream
 *
 */
public class Window2 extends JFrame {
	private JButton btn1;
	private JButton btn2;
	private JButton btn3;
	private JButton btn4;
	private JButton btn5;
	private JPanel panel;
	private JLabel label;
	
	public void Init(){
		
		Container jpanel=this.getContentPane();
		
		btn1=new JButton();
		btn1.setPreferredSize(new Dimension(100, 50));
		btn1.setText("Button1");
		jpanel.add(btn1,BorderLayout.NORTH);
		
		
		btn2=new JButton();
		btn2.setText("Button2");
		jpanel.add(btn2,BorderLayout.SOUTH);
		
		btn3=new JButton();
		btn3.setText("Button3");
		jpanel.add(btn3,BorderLayout.WEST);
		
		btn4=new JButton();
		btn4.setText("Button4");
		jpanel.add(btn4,BorderLayout.EAST);
		
		btn5=new JButton();
		btn5.setText("Button5");
		btn5.setSize(100,50);
		panel=new JPanel();
		panel.setBackground(Color.green);
		panel.add(btn5);
		
		label=new JLabel();
		label.setText("边界布局中嵌套Panel流式布局");
		panel.add(label);
		
		
		panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel.setPreferredSize(new Dimension(200, 100));
		
		
		
		jpanel.add(panel,BorderLayout.EAST);
		
		
		
		
	}
	
	public void Show(){
		this.setVisible(true);
	}
}

package javagui.ui;

import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
/**
 * 网格布局GridLayout
 * 布局效果:将容器划分为规则的矩形网格,每个单元格大小相等.
Public GridLayout();默认设置,所有组件在同一行中,各占一列.
Public GridLayout(int rows,int cols): 指定行数和列数.
Public GridLayout(int rows,int cols, int h,int v):指定行数,列数和垂直,水平间距.
 * @author dream
 *
 */
public class Window3 extends JFrame {
	private JButton btn1;
	private JButton btn2;
	private JButton btn3;
	private JButton btn4;
	
	public void Init(){
		Container c=this.getContentPane();
		c.setLayout(new GridLayout(2, 2));
		
		btn1=new JButton();
		btn1.setText("Button1");
		c.add(btn1);
		
		btn2=new JButton();
		btn2.setText("Button2");
		c.add(btn2);
		
		btn3=new JButton();
		btn3.setText("Button3");
		c.add(btn3);
		
		btn4=new JButton();
		btn4.setText("Button4");
		c.add(btn4);
		
		
	}
	
	public void Show(){
		this.setVisible(true);
	}
}

猜你喜欢

转载自blog.csdn.net/vs2008aspnet/article/details/37561723