第11章 GUI 02 GUI 初始 Frame and Panel2
第11章 GUI 02 GUI 初步 Frame and Panel2
鱼欲遇雨:每日都学习一点,持之以恒,天道酬勤!不能用电脑时,提前补上!(2012.9.6)
Panel
1 Panel对象可以看成可以容纳Component的空间
2 Panel对象可以拥有自己的布局管理器
3 Panel类拥有从其父类继承类的
----------setBounds(int x, int y , int width, int height)
----------setSize(int width, int height)
----------setLocation(int x, int y)
----------setBackground(Color c)
----------setLayout(LayoutManager mgr)等方法
4 Panel的构造方法为:
----------Panel()使用默认的FlowLayout类布局管理器初始化。
----------Panel(LayoutManager layout)使用指定的布局管理器初始化。
代码示例:
// TestPanel.java 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); } }
// TestMultiPanel.java import java.awt.*; public class TestMultiPanel { public static void main(String args[]) { MyFrame2 f = new MyFrame2("MyFrame With Panel", 300, 300, 400, 300); } } class MyFrame2 extends Frame { static 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.RED); p2.setBackground(Color.GREEN); p3.setBackground(Color.YELLOW); p4.setBackground(Color.BLUE); add(p1); add(p2); add(p3); add(p4); setBounds(x, y, w, h); setVisible(true); } }
设计一个含有Panel的自定义的Frame类含有,形式如下图: