Java图形界面设计

一、总述

Java的图形用户界面由各种组件(component)构成,它们主要位于java.awt包与javax.swing包中。Swing与AWT最大的不同是,Swing在实现时,不包含任何本地代码(native),是一种“轻量级(lightweight)”的组件

Swing具有状态的组件。

二、容器

1.顶层容器:

JFrame、JApplet、JDialog 和 JWindow

2.JFrame创建的一个程序

2.1代码

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.   
  4. publicclass JFrameDemo{ 
  5.     publicstaticvoid main(String args[]){ 
  6.         JFrame frame = new JFrame("JFrameDemo"); 
  7.         JButton button = new JButton("Press Me"); 
  8.   
  9.         //first way to do that 
  10.         // frame.getContentPane().add(button,BorderLayout.CENTER); 
  11.   
  12.   
  13.         //another way to set the Pane    
  14.         JPanel contentPane = new JPanel(); 
  15.         contentPane.setLayout(new BorderLayout()); 
  16.         contentPane.add(button,BorderLayout.CENTER); 
  17.         frame.setContentPane(contentPane); 
  18.   
  19.         //frame.pack(); 
  20.         frame.setVisible(true); 
  21.     } 
import java.awt.*;
 import javax.swing.*;
 
 public class JFrameDemo{
 	public static void main(String args[]){
 		JFrame frame = new JFrame("JFrameDemo");
 		JButton button = new JButton("Press Me");
 
 		//first way to do that
 		// frame.getContentPane().add(button,BorderLayout.CENTER);
 
 
 		//another way to set the Pane 	
 		JPanel contentPane = new JPanel();
 		contentPane.setLayout(new BorderLayout());
 		contentPane.add(button,BorderLayout.CENTER);
 		frame.setContentPane(contentPane);
 
 		//frame.pack();
 		frame.setVisible(true);
 	}
 }

2.2执行结果

 

3.面板(JPanel)

可以相互嵌套,不能独立存在,只能添加到其他窗口内部。

3.1代码

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.   
  4. publicclass FrameWithPanel{ 
  5.     publicstaticvoid main(String args[]){ 
  6.     JFrame frame = new JFrame("Frame with Panel"); 
  7.     Container contentPane = frame.getContentPane(); 
  8.     contentPane.setBackground(Color.CYAN); 
  9.   
  10.     JPanel panel = new JPanel(); 
  11.     panel.setBackground(Color.yellow); 
  12.   
  13.     JButton button = new JButton("Press me"); 
  14.     panel.add(button); 
  15.     //add JButton instance to JPanel 
  16.   
  17.     //add JPanel instance to JFrame's south 
  18.     contentPane.add(panel,BorderLayout.SOUTH); 
  19.     frame.setSize(300,200); 
  20.     frame.setVisible(true); 
  21.     } 
import java.awt.*;
 import javax.swing.*;
 
 public class FrameWithPanel{
 	public static void main(String args[]){
 	JFrame frame = new JFrame("Frame with Panel");
 	Container contentPane = frame.getContentPane();
 	contentPane.setBackground(Color.CYAN);
 
 	JPanel panel = new JPanel();
 	panel.setBackground(Color.yellow);
 
 	JButton button = new JButton("Press me");
 	panel.add(button);
 	//add JButton instance to JPanel
 
 	//add JPanel instance to JFrame's south
 	contentPane.add(panel,BorderLayout.SOUTH);
 	frame.setSize(300,200);
 	frame.setVisible(true);
 	}
 }

3.2执行结果

 

三、布局

1.总述

组件的布局(包括位置与大小)通常由Layout Manager负责安排。Java平台提供了多种布局管理器,以下对其部分,进行说明。

2.FlowLayout Layout Manager

2.1FlowLayout 的三种构造方法

 

  1. public FlowLayout() 
  2.     public FlowLayout(int align) 
  3.     public FlowLayout(int align,int hgap,int vgap) 
			public FlowLayout()
 			public FlowLayout(int align)
 			public FlowLayout(int align,int hgap,int vgap)

 

构造方法中,提供了一个对齐方式的可选项align,取值有三种形式:FlowLayout.LEFT、 FlowLayout.CENTER 、FlowLayout.RIGHT。hgap和vgap可以设定组件的水平间距和垂直距离。

2.2参考代码

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.   
  4. publicclass FlowLayoutDemo{ 
  5.     private JFrame frame; 
  6.     private JButton btn1,btn2,btn3; 
  7.   
  8.     publicstaticvoid main(String args[]){ 
  9.     FlowLayoutDemo that = new FlowLayoutDemo(); 
  10.     that.go(); 
  11.     } 
  12.     publicvoid go(){ 
  13.     frame = new JFrame("Flow Layout"); 
  14.     Container contentPane = frame.getContentPane(); 
  15.   
  16.     contentPane.setLayout(new FlowLayout()); 
  17.   
  18.     btn1 = new JButton("OK"); 
  19.     btn2 = new JButton("Open"); 
  20.     btn3 = new JButton("Close"); 
  21.   
  22.     contentPane.add(btn1); 
  23.     contentPane.add(btn2); 
  24.     contentPane.add(btn3); 
  25.   
  26.     frame.setSize(300,200); 
  27.     frame.setVisible(true); 
  28.     } 
import java.awt.*;
 import javax.swing.*;
 
 public class FlowLayoutDemo{
 	private JFrame frame;
 	private JButton btn1,btn2,btn3;
 
 	public static void main(String args[]){
 	FlowLayoutDemo that = new FlowLayoutDemo();
 	that.go();
 	}
 	public void go(){
 	frame = new JFrame("Flow Layout");
 	Container contentPane = frame.getContentPane();
 
 	contentPane.setLayout(new FlowLayout());
 
 	btn1 = new JButton("OK");
 	btn2 = new JButton("Open");
 	btn3 = new JButton("Close");
 
 	contentPane.add(btn1);
 	contentPane.add(btn2);
 	contentPane.add(btn3);
 
 	frame.setSize(300,200);
 	frame.setVisible(true);
 	}
 }

2.3执行结果

 

改变Frame的大小,Frame中组件的布局也会随之改变。

3.BorderLayout 布局管理器

3.1概述

BorderLayout是顶层窗口中内容窗格的默认布局管理器,被划分为BorderLayout.NORTH、BorderLayout.SOUTH、BorderLayout.WEST、BorderLayout.EAST、BorderLayout.CENTER 五个区域。

3.2构造函数

 

  1. BorderLayout() 
  2.     BorderLayout(int,int
			BorderLayout()
 			BorderLayout(int,int)

3.3示例代码

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.   
  4. publicclass BorderLayoutDemo{ 
  5.     private JFrame frame; 
  6.     private JButton be,bw,bn,bs,bc; 
  7.   
  8.     publicstaticvoid main(String args[]){ 
  9.     BorderLayoutDemo that = new BorderLayoutDemo(); 
  10.     that.go(); 
  11.     } 
  12.     publicvoid go(){ 
  13.     frame = new JFrame("Border Layout"); 
  14.     be = new JButton("East"); 
  15.     bw = new JButton("West"); 
  16.     bn = new JButton("North"); 
  17.     bs = new JButton("South"); 
  18.     bc = new JButton("Center"); 
  19.   
  20.     frame.getContentPane().add(be,"East"); 
  21.     frame.getContentPane().add(bw,BorderLayout.WEST); 
  22.     frame.getContentPane().add(bn,BorderLayout.NORTH); 
  23.     frame.getContentPane().add(bs,BorderLayout.SOUTH); 
  24.     frame.getContentPane().add(bc,BorderLayout.CENTER); 
  25.   
  26.     frame.setSize(350,200); 
  27.     frame.setVisible(true); 
  28.     } 
import java.awt.*;
 import javax.swing.*;
 
 public class BorderLayoutDemo{
 	private JFrame frame;
 	private JButton be,bw,bn,bs,bc;
 
 	public static void main(String args[]){
 	BorderLayoutDemo that = new BorderLayoutDemo();
 	that.go();
 	}
 	public void go(){
 	frame = new JFrame("Border Layout");
 	be = new JButton("East");
 	bw = new JButton("West");
 	bn = new JButton("North");
 	bs = new JButton("South");
 	bc = new JButton("Center");
 
 	frame.getContentPane().add(be,"East");
 	frame.getContentPane().add(bw,BorderLayout.WEST);
 	frame.getContentPane().add(bn,BorderLayout.NORTH);
 	frame.getContentPane().add(bs,BorderLayout.SOUTH);
 	frame.getContentPane().add(bc,BorderLayout.CENTER);
 
 	frame.setSize(350,200);
 	frame.setVisible(true);
 	}
 }

 

3.4执行结果

 

 

4.GridLayout布局管理器

4.1总述

是一种网格式的布局管理器,将窗口空间分割成若干行,乘若干列的网格。组件依次放入,占一格。

4.2构造函数

public GridLayout()

public GridLayout(int rows, int cols)

public GridLayout(int rows, int cols, int hgap, int vgap)

4.3参考代码

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.   
  4. publicclass GridLayoutDemo{ 
  5.     private JFrame frame; 
  6.     private JButton b1,b2,b3,b4,b5,b6; 
  7.   
  8.     publicstaticvoid main(String args[]){ 
  9.     GridLayoutDemo that = new GridLayoutDemo(); 
  10.     that.go(); 
  11.     } 
  12.     publicvoid go(){ 
  13.     frame = new JFrame("Grid Frame"); 
  14.   
  15.     Container contentPane = frame.getContentPane(); 
  16.   
  17.     contentPane.setLayout(new GridLayout(3,2)); 
  18.   
  19.     b1 = new JButton("grid_1"); 
  20.     b2 = new JButton("grid_2"); 
  21.     b3 = new JButton("grid_3"); 
  22.     b4 = new JButton("grid_4"); 
  23.     b5 = new JButton("grid_5"); 
  24.     b6 = new JButton("grid_6"); 
  25.   
  26.     contentPane.add(b1); 
  27.     contentPane.add(b2); 
  28.     contentPane.add(b3); 
  29.     contentPane.add(b4); 
  30.     contentPane.add(b5); 
  31.     contentPane.add(b6); 
  32.   
  33.     frame.pack(); 
  34.     frame.setVisible(true); 
  35.     } 
import java.awt.*;
 import javax.swing.*;
 
 public class GridLayoutDemo{
 	private JFrame frame;
 	private JButton b1,b2,b3,b4,b5,b6;
 
 	public static void main(String args[]){
 	GridLayoutDemo that = new GridLayoutDemo();
 	that.go();
 	}
 	public void go(){
 	frame = new JFrame("Grid Frame");
 
 	Container contentPane = frame.getContentPane();
 
 	contentPane.setLayout(new GridLayout(3,2));
 
 	b1 = new JButton("grid_1");
 	b2 = new JButton("grid_2");
 	b3 = new JButton("grid_3");
 	b4 = new JButton("grid_4");
 	b5 = new JButton("grid_5");
 	b6 = new JButton("grid_6");
 
 	contentPane.add(b1);
 	contentPane.add(b2);
 	contentPane.add(b3);
 	contentPane.add(b4);
 	contentPane.add(b5);
 	contentPane.add(b6);
 
 	frame.pack();
 	frame.setVisible(true);
 	}
 }

 

4.4执行结果


5.CardLayout 布局管理器

5.1总述

是一种卡片式的布局管理器,它将容器中的组件处理成一系列卡片,每一时刻只显示出其中一张。

5.2参考代码:

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3. import java.awt.event.*; 
  4.   
  5. publicclass CardLayoutDemo extends MouseAdapter{ 
  6.     JPanel p1,p2,p3,p4,p5; 
  7.     JLabel l1, l2, l3, l4, l5; 
  8.   
  9.     //declare a Cardlayout object 
  10.     CardLayout myCard; 
  11.     JFrame frame; 
  12.     Container contentPane; 
  13.   
  14.     publicstaticvoid main(String args[]){ 
  15.     CardLayoutDemo that = new CardLayoutDemo(); 
  16.     that.go(); 
  17.     System.out.println("test"); 
  18.     } 
  19.   
  20.     publicvoid go(){ 
  21.     frame = new JFrame("Card Test"); 
  22.     contentPane = frame.getContentPane(); 
  23.     myCard = new CardLayout(); 
  24.   
  25.     contentPane.setLayout(myCard); 
  26.   
  27.     p1=new JPanel(); 
  28.     p2 = new JPanel(); 
  29.     p3 = new JPanel(); 
  30.     p4 = new JPanel(); 
  31.     p5 = new JPanel(); 
  32.   
  33.     //set the different bk color for each JPanel label 
  34.     l1 =  new JLabel("This is the first JPanel"); 
  35.     p1.add(l1); 
  36.     p1.setBackground(Color.yellow); 
  37.   
  38.     l2 =  new JLabel("This is the second JPanel"); 
  39.     p2.add(l2); 
  40.     p2.setBackground(Color.green); 
  41.   
  42.     l3 =  new JLabel("This is the third JPanel"); 
  43.     p3.add(l3); 
  44.     p3.setBackground(Color.magenta); 
  45.   
  46.     l4 =  new JLabel("This is the forth JPanel"); 
  47.     p4.add(l4); 
  48.     p4.setBackground(Color.white); 
  49.   
  50.     l5 =  new JLabel("This is the fifth JPanel"); 
  51.     p5.add(l5); 
  52.     p5.setBackground(Color.cyan); 
  53.   
  54.     //set mouseListener 
  55.     p1.addMouseListener(this); 
  56.     p2.addMouseListener(this); 
  57.     p3.addMouseListener(this); 
  58.     p4.addMouseListener(this); 
  59.     p5.addMouseListener(this); 
  60.   
  61.     //set each JPanle as a Card to insert frame 
  62.     contentPane.add(p1,"First"); 
  63.     contentPane.add(p2,"Sencond"); 
  64.     contentPane.add(p3,"Third"); 
  65.     contentPane.add(p4,"Forth"); 
  66.     contentPane.add(p5,"Fifth"); 
  67.   
  68.     //display the first pic 
  69.     myCard.show(contentPane,"First"); 
  70.     frame.setSize(300,200); 
  71.      frame.show(); 
  72.     } 
  73.   
  74.     publicvoid mouseClicked(MouseEvent e){ 
  75.     myCard.next(contentPane); 
  76.     } 
import java.awt.*;
 import javax.swing.*;
 import java.awt.event.*;
 
 public class CardLayoutDemo extends MouseAdapter{
 	JPanel p1,p2,p3,p4,p5;
 	JLabel l1, l2, l3, l4, l5;
 
 	//declare a Cardlayout object
 	CardLayout myCard;
 	JFrame frame;
 	Container contentPane;
 
 	public static void main(String args[]){
 	CardLayoutDemo that = new CardLayoutDemo();
 	that.go();
 	System.out.println("test");
 	}
 
 	public void go(){
 	frame = new JFrame("Card Test");
 	contentPane = frame.getContentPane();
 	myCard = new CardLayout();
 
 	contentPane.setLayout(myCard);
 
 	p1=new JPanel();
 	p2 = new JPanel();
 	p3 = new JPanel();
 	p4 = new JPanel();
 	p5 = new JPanel();
 
 	//set the different bk color for each JPanel label
 	l1 =  new JLabel("This is the first JPanel");
 	p1.add(l1);
 	p1.setBackground(Color.yellow);
 
 	l2 =  new JLabel("This is the second JPanel");
 	p2.add(l2);
 	p2.setBackground(Color.green);
 
 	l3 =  new JLabel("This is the third JPanel");
 	p3.add(l3);
 	p3.setBackground(Color.magenta);
 
 	l4 =  new JLabel("This is the forth JPanel");
 	p4.add(l4);
 	p4.setBackground(Color.white);
 
 	l5 =  new JLabel("This is the fifth JPanel");
 	p5.add(l5);
 	p5.setBackground(Color.cyan);
 
 	//set mouseListener
 	p1.addMouseListener(this);
 	p2.addMouseListener(this);
 	p3.addMouseListener(this);
 	p4.addMouseListener(this);
 	p5.addMouseListener(this);
 
 	//set each JPanle as a Card to insert frame
 	contentPane.add(p1,"First");
 	contentPane.add(p2,"Sencond");
 	contentPane.add(p3,"Third");
 	contentPane.add(p4,"Forth");
 	contentPane.add(p5,"Fifth");
 
 	//display the first pic
 	myCard.show(contentPane,"First");
 	frame.setSize(300,200);
 	 frame.show();
 	}
 
 	public void mouseClicked(MouseEvent e){
 	myCard.next(contentPane);
 	}
 }

 

5.2执行结果

 


 

6.BoxLayout布局管理器

6.1总述

将容器中的组件,按水平排成一行,或按垂直方向排成一列。

6.2构造函数

BoxLayout(Container target, int axis)

axis = BoxLayout.X_AXIS | BoxLayout.Y_AXIS

6.3参考代码

 

import java.awt.*;

import javax.swing.*;

public class BoxLayoutDemo{

private JFrame frame;

private JPanel pv,ph;

public static void main(String args[]){

BoxLayoutDemo that = new BoxLayoutDemo();

that.go();

}

public void go(){

frame = new JFrame("Box Layout example");

Container contentPane = frame.getContentPane();

  pv = new JPanel();

pv.setLayout(new BoxLayout(pv,BoxLayout.Y_AXIS));

  pv.add(new JLabel("Monday"));

pv.add(new JLabel("Tuesday"));

pv.add(new JLabel("Wednesday"));

pv.add(new JLabel("Thursday"));

pv.add(new JLabel("Friday"));

pv.add(new JLabel("Saturday"));

pv.add(new JLabel("Sunday"));   

contentPane.add(pv,BorderLayout.CENTER); 

ph  = new JPanel();  

ph.setLayout(new BoxLayout(ph,BoxLayout.X_AXIS)); 

ph.add(new JButton("Yes"));

ph.add(new JButton("No"));

ph.add(new JButton("Cancle"));

contentPane.add(ph,BorderLayout.SOUTH); 

frame.pack();

frame.setVisible(true);

}

}

 

6.4执行结果

 


6.5专门使用BoxLayout的特殊容器—Box类

6.5.1创建实例的静态方法

 

  1. publicstatic Box createHorizontalBox() 
  2.     publicstatic Box createVerticalBox() 
		public static Box createHorizontalBox()
 		public static Box createVerticalBox()

6.5.2|6.3的代码,改写如下

 

  1. //the specific Box class for BoxLayout Container 
  2. import java.awt.*; 
  3. import javax.swing.*; 
  4.   
  5. publicclass BoxDemo{ 
  6.     private JFrame frame; 
  7.     private Box bv,bh; 
  8.   
  9.     publicstaticvoid main(String arg[]){ 
  10.         BoxDemo that = new BoxDemo(); 
  11.         that.go(); 
  12.     } 
  13.     void go(){ 
  14.         frame = new JFrame("Box Layout example"); 
  15.         Container contentPane = frame.getContentPane(); 
  16.   
  17.         bv = Box.createVerticalBox(); 
  18.   
  19.         bv.add(new JLabel("Monday")); 
  20.         bv.add(new JLabel("Tuesday")); 
  21.         bv.add(new JLabel("Wednesday")); 
  22.         bv.add(new JLabel("Thursday")); 
  23.         bv.add(new JLabel("Friday")); 
  24.         bv.add(new JLabel("Saturday")); 
  25.         bv.add(new JLabel("Sunday")); 
  26.   
  27.         contentPane.add(bv,BorderLayout.CENTER); 
  28.   
  29.         bh = Box.createHorizontalBox(); 
  30.         bh.add(new JButton("Yes")); 
  31.         bh.add(new JButton("No")); 
  32.         bh.add(new JButton("Cancel")); 
  33.   
  34.         contentPane.add(bh,BorderLayout.SOUTH); 
  35.   
  36.         frame.setSize(300,200); 
  37.         //frame.pack(); 
  38.         frame.setVisible(true); 
  39.     } 
//the specific Box class for BoxLayout Container
 import java.awt.*;
 import javax.swing.*;
 
 public class BoxDemo{
 	private JFrame frame;
 	private Box bv,bh;
 
 	public static void main(String arg[]){
 		BoxDemo that = new BoxDemo();
 		that.go();
 	}
 	void go(){
 		frame = new JFrame("Box Layout example");
 		Container contentPane = frame.getContentPane();
 
 		bv = Box.createVerticalBox();
 
 		bv.add(new JLabel("Monday"));
 		bv.add(new JLabel("Tuesday"));
 		bv.add(new JLabel("Wednesday"));
 		bv.add(new JLabel("Thursday"));
 		bv.add(new JLabel("Friday"));
 		bv.add(new JLabel("Saturday"));
 		bv.add(new JLabel("Sunday"));
 
 		contentPane.add(bv,BorderLayout.CENTER);
 
 		bh = Box.createHorizontalBox();
 		bh.add(new JButton("Yes"));
 		bh.add(new JButton("No"));
 		bh.add(new JButton("Cancel"));
 
 		contentPane.add(bh,BorderLayout.SOUTH);
 
 		frame.setSize(300,200);
 		//frame.pack();
 		frame.setVisible(true);
 	}
 }

 

6.6Box类提供的创建不可组件方法

 

 

  1. publicstatic Component createHorizontalGlue() 
  2.     publicstatic Component createHorizontalStrut(int width) 
  3.     publicstatic Component createVerticalGlue() 
  4.     publicstatic Component createVerticalStrut(int height) 
  5.     publicstatic Component createRigidArea(Dimension d) 
	public static Component createHorizontalGlue()
 	public static Component createHorizontalStrut(int width)
 	public static Component createVerticalGlue()
 	public static Component createVerticalStrut(int height)
 	public static Component createRigidArea(Dimension d)

6.6.1参考代码

 

  1. //to create invisible component 
  2. import java.awt.*; 
  3. import javax.swing.*; 
  4.   
  5. publicclass GlueAndStrut{ 
  6.     private JFrame frame; 
  7.     private Box b1,b2,b3,b4; 
  8.   
  9.     publicstaticvoid main(String args[]){ 
  10.         GlueAndStrut that = new GlueAndStrut(); 
  11.         that.go(); 
  12.     } 
  13.     void go(){ 
  14.         frame = new JFrame("Glue And Strut"); 
  15.         Container contentPane = frame.getContentPane(); 
  16.         contentPane.setLayout(new GridLayout(4,1)); 
  17.   
  18.         b1 = Box.createHorizontalBox(); 
  19.         b1.add(new JLabel("Box 1:")); 
  20.         b1.add(new JButton("Yes")); 
  21.         b1.add(new JButton("No")); 
  22.         b1.add(new JButton("Cancel")); 
  23.   
  24.         b2 = Box.createHorizontalBox(); 
  25.         b2.add(new JLabel("Box 2:")); 
  26.         b2.add(new JButton("Yes")); 
  27.         b2.add(new JButton("No")); 
  28.         b2.add(Box.createHorizontalGlue()); 
  29.         b2.add(new JButton("Cancel")); 
  30.   
  31.         b3 = Box.createHorizontalBox(); 
  32.         b3.add(new JLabel("Box 3:")); 
  33.         b3.add(new JButton("Yes")); 
  34.         b3.add(new JButton("No")); 
  35.         b3.add(Box.createHorizontalStrut(20)); 
  36.         b3.add(new JButton("Cancel")); 
  37.   
  38.         b4 = Box.createHorizontalBox(); 
  39.         b4.add(new JLabel("Box 4:")); 
  40.         b4.add(new JButton("Yes")); 
  41.         b4.add(new JButton("No")); 
  42.         b4.add(Box.createRigidArea(new Dimension(10,10))); 
  43.         b4.add(new JButton("Cancel")); 
  44.   
  45.         contentPane.add(b1); 
  46.         contentPane.add(b2); 
  47.         contentPane.add(b3); 
  48.         contentPane.add(b4); 
  49.   
  50.         frame.setSize(300,200); 
  51.         frame.setVisible(true); 
  52.     } 
//to create invisible component
 import java.awt.*;
 import javax.swing.*;
 
 public class GlueAndStrut{
 	private JFrame frame;
 	private Box b1,b2,b3,b4;
 
 	public static void main(String args[]){
 		GlueAndStrut that = new GlueAndStrut();
 		that.go();
 	}
 	void go(){
 		frame = new JFrame("Glue And Strut");
 		Container contentPane = frame.getContentPane();
 		contentPane.setLayout(new GridLayout(4,1));
 
 		b1 = Box.createHorizontalBox();
 		b1.add(new JLabel("Box 1:"));
 		b1.add(new JButton("Yes"));
 		b1.add(new JButton("No"));
 		b1.add(new JButton("Cancel"));
 
 		b2 = Box.createHorizontalBox();
 		b2.add(new JLabel("Box 2:"));
 		b2.add(new JButton("Yes"));
 		b2.add(new JButton("No"));
 		b2.add(Box.createHorizontalGlue());
 		b2.add(new JButton("Cancel"));
 
 		b3 = Box.createHorizontalBox();
 		b3.add(new JLabel("Box 3:"));
 		b3.add(new JButton("Yes"));
 		b3.add(new JButton("No"));
 		b3.add(Box.createHorizontalStrut(20));
 		b3.add(new JButton("Cancel"));
 
 		b4 = Box.createHorizontalBox();
 		b4.add(new JLabel("Box 4:"));
 		b4.add(new JButton("Yes"));
 		b4.add(new JButton("No"));
 		b4.add(Box.createRigidArea(new Dimension(10,10)));
 		b4.add(new JButton("Cancel"));
 
 		contentPane.add(b1);
 		contentPane.add(b2);
 		contentPane.add(b3);
 		contentPane.add(b4);
 
 		frame.setSize(300,200);
 		frame.setVisible(true);
 	}
 }

 

6.6.2执行结果

7.不使用布局管理器

7.1一般做法

a.setLayout(null)将布局管理器设置为空

b.setBounds(int x,int y,int width,int height)设置组件的位置与大小

7.2参考代码

 

  1. import java.awt.*; 
  2. import javax.swing.*; 
  3.   
  4. publicclass NullLayoutDemo{ 
  5.     private JFrame frame; 
  6.     private JButton b1,b2,b3; 
  7.   
  8.     publicstaticvoid main(String args[]){ 
  9.         NullLayoutDemo that = new NullLayoutDemo(); 
  10.         that.go(); 
  11.     } 
  12.     privatevoid go(){ 
  13.         frame = new JFrame("Null Layout Demo"); 
  14.         Container contentPane = frame.getContentPane(); 
  15.   
  16.         //set the layout as null;    
  17.         contentPane.setLayout(null); 
  18.   
  19.         //add the buttons 
  20.         b1 = new JButton("Yes"); 
  21.         contentPane.add(b1); 
  22.         b2 = new JButton("No"); 
  23.         contentPane.add(b2); 
  24.         b3 = new JButton("Cancel"); 
  25.         contentPane.add(b3); 
  26.   
  27.         //set the buttons' pos and size; 
  28.         b1.setBounds(30,15,75,20); 
  29.         b2.setBounds(150,15,75,50); 
  30.         b3.setBounds(150,100,75,20); 
  31.   
  32.         frame.setSize(300,200); 
  33.         frame.setVisible(true); 
  34.     } 
import java.awt.*;
 import javax.swing.*;
 
 public class NullLayoutDemo{
 	private JFrame frame;
 	private JButton b1,b2,b3;
 
 	public static void main(String args[]){
 		NullLayoutDemo that = new NullLayoutDemo();
 		that.go();
 	}
 	private void go(){
 		frame = new JFrame("Null Layout Demo");
 		Container contentPane = frame.getContentPane();
 
 		//set the layout as null;	
 		contentPane.setLayout(null);
 
 		//add the buttons
 		b1 = new JButton("Yes");
 		contentPane.add(b1);
 		b2 = new JButton("No");
 		contentPane.add(b2);
 		b3 = new JButton("Cancel");
 		contentPane.add(b3);
 
 		//set the buttons' pos and size;
 		b1.setBounds(30,15,75,20);
 		b2.setBounds(150,15,75,50);
 		b3.setBounds(150,100,75,20);
 
 		frame.setSize(300,200);
 		frame.setVisible(true);
 	}
 }

 

7.3执行结果

posted @ 2013-12-28 21:14  菜鸟搞IT  阅读(3327)  评论(0编辑  收藏  举报