标签:
/* 范例名称: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); } }
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); } }
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); } }
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); } }
练习
import java.awt.*; public class CenterPanel { public static void main(String args[]) { new MyFrame3(300,300,400,300,Color.BLUE); } } class MyFrame3 extends Frame{ private Panel p; MyFrame3(int x,int y,int w,int h,Color c){ super("FrameWithPanel"); setLayout(null); setBounds(x,y,w,h); setBackground(c); p = new Panel(null); p.setBounds(w/4,h/4,w/2,h/2); p.setBackground(Color.YELLOW); add(p); setVisible(true); } }
/* 范例名称:FlowLayout 用法举例 * 源文件名称:TestFlowLayout.java * 要 点: * 1. 布局管理器的概念和作用 * 2. FlowLayout的性质及用法 */ 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); } }
import java.awt.*; public class TestFlowLayout2 { public static void main(String args[]) { Frame f = new Frame("Java Frame"); 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应用举例 * 源文件名称:TestBorderLayout.java * 要 点:FlowLayout布局管理器的性质及用法 */ 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, "North"); f.add(bs, "South"); f.add(bw, "West"); f.add(be, "East"); f.add(bc, "Center"); // 也可使用下述语句 /* 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应用举例 * 源文件名称:TestGridLayout * 要 点:GridLayout布局管理器的性质及用法 */ import java.awt.*; public class TestGridLayout { public static void main(String args[]) { Frame f = new Frame("GridLayout 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(); f.setVisible(true); } }
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("BUTTON"),BorderLayout.WEST); p1.add(new Button("BUTTON"),BorderLayout.EAST); p11.add(new Button("BUTTON")); p11.add(new Button("BUTTON")); p1.add(p11,BorderLayout.CENTER); p2.add(new Button("BUTTON"),BorderLayout.WEST); p2.add(new Button("BUTTON"),BorderLayout.EAST); for(int i =1;i<=4;i++) {p21.add(new Button("BUTTON"));} p2.add(p21,BorderLayout.CENTER); f.add(p1);f.add(p2); f.setVisible(true); } }
标签:
原文地址:http://www.cnblogs.com/gimin/p/4790361.html