标签:
1 /* 范例名称:Frame 应用举例 2 * 源文件名称:TestFrame.java 3 * 要 点:Frame组件的创建及显示设置 4 */ 5 6 import java.awt.*; 7 public class TestFrame { 8 public static void main( String args[]) { 9 Frame f = new Frame("My First Test"); 10 f.setLocation(500, 200); 11 f.setSize( 170,100); 12 f.setBackground( Color.green); 13 f.setResizable(true); 14 f.setVisible( true); 15 } 16 }
1 import java.awt.*; 2 3 public class TestMultiFrame { 4 public static void main(String args[]) { 5 MyFrame f1 = 6 new MyFrame(100,100,200,200,Color.BLUE); 7 MyFrame f2 = 8 new MyFrame(300,100,200,200,Color.YELLOW); 9 MyFrame f3 = 10 new MyFrame(100,300,200,200,Color.GREEN); 11 MyFrame f4 = 12 new MyFrame(300,300,200,200,Color.MAGENTA); 13 } 14 } 15 16 17 18 19 class MyFrame extends Frame{ 20 static int id = 0; 21 MyFrame(int x,int y,int w,int h,Color color){ 22 super("MyFrame " + (++id)); 23 setBackground(color); 24 setLayout(null); 25 setBounds(x,y,w,h); 26 setVisible(true); 27 } 28 }
1 import java.awt.*; 2 3 public class TestPanel { 4 public static void main(String args[]) { 5 Frame f = 6 new Frame("Java Frame with Panel"); 7 Panel p = new Panel(null); 8 f.setLayout(null); 9 f.setBounds(300,300,500,500); 10 f.setBackground(new Color(0,0,102)); 11 p.setBounds(50,50,400,400); 12 p.setBackground(new Color(204,204,255)); 13 f.add(p); 14 f.setVisible(true); 15 } 16 }
1 import java.awt.*; 2 3 public class TestMultiPanel { 4 public static void main(String args[]) { 5 new MyFrame2("MyFrameWithPanel",300,300,400,300); 6 } 7 } 8 9 10 class MyFrame2 extends Frame{ 11 private Panel p1,p2,p3,p4; 12 MyFrame2(String s,int x,int y,int w,int h){ 13 super(s); 14 setLayout(null); 15 p1 = new Panel(null); p2 = new Panel(null); 16 p3 = new Panel(null); p4 = new Panel(null); 17 p1.setBounds(0,0,w/2,h/2); 18 p2.setBounds(0,h/2,w/2,h/2); 19 p3.setBounds(w/2,0,w/2,h/2); 20 p4.setBounds(w/2,h/2,w/2,h/2); 21 p1.setBackground(Color.BLUE); 22 p2.setBackground(Color.GREEN); 23 p3.setBackground(Color.YELLOW); 24 p4.setBackground(Color.MAGENTA); 25 add(p1);add(p2);add(p3);add(p4); 26 setBounds(x,y,w,h); 27 setVisible(true); 28 } 29 }
标签:
原文地址:http://www.cnblogs.com/roger-h/p/4510591.html