马士兵 J2SE 第11章 GUI编程 GUI初步和布局管理器

/*	范例名称:Frame 应用举例
 * 	源文件名称:TestFrame.java
 *	要  点:Frame组件的创建及显示设置
 */

import java.awt.*;
public class TestFrame {
	public static void main( String args[]) {
		Frame f = new Frame("My First Test");
		f.setLocation(300, 300);
		f.setSize( 170,100);
		f.setBackground( Color.blue);
		f.setResizable(false);
		f.setVisible( true);
	}
}

关闭:

暂时使用CTRL+C关闭


多重FRAME

package com.zzk.cn;

import java.awt.*;

public class TestMultiFrame {
	public static void main(String args[]) {
		MyFrame f1=new MyFrame(100,100,200,200,Color.BLUE);
		MyFrame f2=new MyFrame(300,100,200,200,Color.YELLOW);
		MyFrame f3=new MyFrame(100,300,200,200,Color.GREEN);
		MyFrame f4=new MyFrame(300,300,200,200,Color.MAGENTA);
		
	}
}


class MyFrame extends Frame {
	static int id=0;
	MyFrame(int x,int y,int w,int h,Color color) {
		super("MyFrame "+(++id));
		setBackground(color);
		setLayout(null);
		setBounds(x,y,w,h);
		setVisible(true);
	}
}



PANEL应用举例

package com.zzk.cn;
import java.awt.*;

public class TestPanel {
	public static void main(String args[]) {
		Frame f=new Frame("Java Frame with Panel");
		Panel p=new Panel(null);
		f.setLayout(null);
		f.setBounds(300,300,500,500);
		f.setBackground(new Color(0,0,102));
		
		
		p.setBounds(50,50,400,400);
		p.setBackground(new Color(204,204,255));
		f.add(p);
		
		f.setVisible(true);
	}
}


多重PANEL


package com.zzk.cn;

import java.awt.*;

public class TestMultiPanel {
	public static void main(String[] args) {
		new MyFrame2("MyFrameWithPanel",300,300,400,300);
	}
}

class MyFrame2 extends Frame {
	private Panel p1,p2,p3,p4;
	MyFrame2(String s,int x,int y,int w,int h) {
		super(s);
		setLayout(null);
		
		p1=new Panel(null);
		p2=new Panel(null);
		p3=new Panel(null);
		p4=new Panel(null);
		
		p1.setBounds(0,0,w/2,h/2);
		p2.setBounds(0,h/2,w/2,h/2);
		p3.setBounds(w/2,0,w/2,h/2);
		p4.setBounds(w/2,h/2,w/2,h/2);
		
		p1.setBackground(Color.BLUE);
		p2.setBackground(Color.GREEN);
		p3.setBackground(Color.YELLOW);
		p4.setBackground(Color.MAGENTA);
		
		add(p1);
		add(p2);
		add(p3);
		add(p4);
		
		setBounds(x,y,w,h);
		setVisible(true);
	}
}


习题:设计一个含有Panel的自定义的Frame类,Panel位于中间

我的代码如下:

package com.zzk.cn;

import java.awt.*;

public class CenterPanel {
	public static void main(String[] args) {
		new MyFrame3("MyFrameWithPanel",300,300,300,300);
	}
}

class MyFrame3 extends Frame {
	private Panel p1;
	MyFrame3(String s,int x,int y,int w,int h) {
		super(s);
		setLayout(null);
		
		p1=new Panel(null);
		
		setBounds(x,y,w,h);
		p1.setBounds(x/4,y/4,w/2,h/2);

		
		p1.setBackground(Color.YELLOW);
		
		add(p1);

		
		setBounds(x,y,w,h);
		setBackground(Color.blue);
		setVisible(true);
	}
}

马士兵的代码如下:

package com.zzk.cn;

import java.awt.*;

public class CenterPanelDemo {
	public static void main(String[] args) {
		new MyFrame4(300,300,400,300,Color.BLUE);
	}
}

class MyFrame4 extends Frame {
	private Panel p;
	
	MyFrame4(int x,int y,int w,int h,Color c) {
		super("FrameWithPanel");
		setLayout(null);
		
		p=new Panel(null);
		
		setBounds(x,y,w,h);
		setBackground(c);
		
		p.setBounds(x/4,y/4,w/2,h/2);
		p.setBackground(Color.YELLOW);
		
		add(p);

		
		setBounds(x,y,w,h);
		setBackground(Color.blue);
		setVisible(true);
	}
}



从马士兵代码中学到的:

PANEL提取出属性,方便以后程序的扩展

参数的传递


FlowLayout布局管理器,流水线式布局

package com.zzk.cn;

import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
    	Frame f=new Frame("Flow Layout");
    	Button button1=new Button("OK");
    	Button button2=new Button("Open");
    	Button button3=new Button("Close");
    
    	f.setLayout(new FlowLayout());
    	f.add(button1);
    	f.add(button2);
    	f.add(button3);
    	f.setSize(100,100);
    	f.setVisible(true);
    }
}


指定左对齐

package com.zzk.cn;

import java.awt.*;

public class TestFlowLayout {
    public static void main(String[] args) {
    	Frame f=new Frame("Flow Layout");
    	Button button1=new Button("OK");
    	Button button2=new Button("Open");
    	Button button3=new Button("Close");
    
    	f.setLayout(new FlowLayout(FlowLayout.LEFT));
    	f.add(button1);
    	f.add(button2);
    	f.add(button3);
    	f.setSize(100,100);
    	f.setVisible(true);
    }
}


第二个FLOWLAYOUT程序示例

package com.zzk.cn;

import java.awt.*;

public class TestFlowLayout2 {
    public static void main(String[] args) {
    	Frame f=new Frame("Java Frame");
    	//中间间距20,垂直间距40
    	FlowLayout l=new FlowLayout(FlowLayout.CENTER,20,40);
    	
    	f.setLayout(l);
    	f.setLocation(300,400);
    	f.setSize(300,200);
    	f.setBackground(new Color(204,204,255));
    	for(int i=1;i<=7;i++) {
    		f.add(new Button("BUTTON"));
    	}
    	f.setVisible(true);
    }
}

能在编译间出现的错误,不要出现在运行间

BorderLayout布局管理器

package com.zzk.cn;

import java.awt.*;

public class TestBorderLayout {
    public static void main(String[] args) {
    	Frame f;
    	f=new Frame("Border Layout");
    	Button bn=new Button("BN");
    	Button bs=new Button("BS");
    	Button bw=new Button("BW");
    	Button be=new Button("BE");
    	Button bc=new Button("BC");
    	
    	f.add(bn,BorderLayout.NORTH);
    	f.add(bs,BorderLayout.SOUTH);
    	f.add(bw,BorderLayout.WEST);
    	f.add(be,BorderLayout.EAST);
    	f.add(bc,BorderLayout.CENTER);
    	
    	f.setSize(200,200);
    	f.setVisible(true);
    }
}


GRIDLAYOUT布局管理器

package com.zzk.cn;

import java.awt.*;

public class TestGridLayout {
    public static void main(String[] args) {
    	Frame f=new Frame("GirdLayout example");
    	
    	Button b1=new Button("b1");
    	Button b2=new Button("b2");
    	Button b3=new Button("b3");
    	Button b4=new Button("b4");
    	Button b5=new Button("b5");
    	Button b6=new Button("b6");
    	
    	f.setLayout(new GridLayout(3,2));
    	
    	f.add(b1);
    	f.add(b2);
    	f.add(b3);
    	f.add(b4);
    	f.add(b5);
    	f.add(b6);
    	
    	f.pack();//不写PACK就不显示,打包
    	f.setVisible(true);
    	
    }
}


习题

package com.zzk.cn;

import java.awt.*;

public class TenButtons {
    public static void main(String[] args) {
    	
    	Frame f=new Frame("Java Frame");
    	f.setLayout(new GridLayout(2,1));//两行一列
    	f.setLocation(300,400);
    	f.setSize(300,200);
    	f.setBackground(new Color(204,204,255));
    	
    	Panel p1=new Panel(new BorderLayout());
    	Panel p2=new Panel(new BorderLayout());
    	
    	Panel p11=new Panel(new GridLayout(2,1));
    	Panel p21=new Panel(new GridLayout(2,2));
    	
    	p1.add(new Button("1"),BorderLayout.WEST);
    	p1.add(new Button("1"),BorderLayout.EAST);
    	
    	p11.add(new Button("2"));
    	p11.add(new Button("3"));
    	
    	p1.add(p11,BorderLayout.CENTER);
    	p2.add(new Button("4"),BorderLayout.WEST);
    	p2.add(new Button("5"),BorderLayout.EAST);
    	
    	for(int i=1;i<=4;i++) {
    		p21.add(new Button("6"));
    	}
    	
    	p2.add(p21,BorderLayout.CENTER);
    	
    	
    	f.add(p1);
    	f.add(p2);
    	f.setVisible(true);
    }
}








你可能感兴趣的:(编程,String,J2SE,null,Class,button)