1,java awt
Container(容器):Window(Frame),Panel
Component(组件):Button,,,
Others:Layout,Color,,,
2,Container(容器)
1) Frame
public class FrameTest { public static void main(String[] args) { Frame f1 = new Frame(); Frame f2 = new Frame(); f1.setSize(100, 200); f2.setSize(200,100); f1.setVisible(true); f2.setVisible(false); } }
2) Panel
import java.awt.*; import java.applet.*; public class AppletPanelTest extends Applet//注意Panel的实现方式 { public void init() { Panel p = new Panel(); p.setBackground(Color.red); p.setSize(100,100); setBackground(Color.green); setSize(200,200); add(p); } }
3,版面配置
1)BorderLayout(东西南北中)
Frame f = new Frame("BorderLayout test");
Button b1 = new Button("One");
f.add(b1, BorderLayout.EAST);
2)FlowLayout(如果宽度够,自动放第一行,不够的话往第二行放)
3)CardLayout
4)GridLayout
Frame f = new Frame("GridLayout test");
f.setLayout(new GridLayout(2,3));//两行三列
5)GridBagLayout 设计你自己的版式
import java.awt.*; public class GridBagLayoutStep1 { public static void main(String argv[]) { Frame f = new Frame("GridBagLayout Step 1"); f.setLayout(new GridBagLayout()); Button b[] = new Button[4]; int att[][] = { {0, 0, 1, 1}, {1, 0, 1, 1}, {2, 0, 1, 1}, {0, 1, 3, 1} }; for (int i=0; i<b.length; i++) { b[i] = new Button("Button "+(i+1)); add(f, b[i], att[i]); } f.pack(); f.setVisible(true); } private static void add(Container con, Component com, int att[])//在容器con中按照限制要求的数组att来添加组件com { GridBagConstraints cons = new GridBagConstraints();//得到一个限制对象 cons.gridx = att[0]; cons.gridy = att[1]; cons.gridwidth = att[2]; cons.gridheight = att[3]; con.add(com, cons);//在容器中按照限制cons添加组件com } }