1.包:ava.awt.*及其子包 container和component是AWT中的两个核心类 所有能显示的图形元素都是component,例如button、label、textarea component不能够独立显示,必须放在container里面 2.Frame是window的子类 构造方法:Frame()\Frame(String s)
Frame f=new Frame("this is my Frame" );
f.setLocation(300 ,300 );
f.setSize(170 ,100 );
f.setBackground(Color.blue);
f.setResizable(false );
f.setVisible(true );
class MyFrame extendsFrame {
MyFrame(){
super ("MyFrame" );
}
}
Frame f=new Frame("this is my Frame" );
Panel p = new Panel();
f.setLayout(null );
f.setBounds(300 ,300 ,300 ,300 );
f.setBackground(Color.blue);
p.setBounds(50 , 50 ,50 ,50 );
p.setBackground(new Color(255 ,255 ,255 ));
f.add(p);
f.setVisible(true );
5.布局管理器 ①FlowLayout 从左到右,从上到下
Frame f = new Frame ();
f.setLayout(new FlowLayout(FlowLayout.LEFT));
Button btn1= new Button ("按钮一" );
Button btn2= new Button ("按钮二" );
f.add(btn1);f.add(btn2);
f.setBounds(200 , 200 , 400 , 400 );
f.setVisible(true);
②BorderLayout
f.add (btn1,BorderLayout.SOUTH )
③GridLayout
f.setLayout(new GridLayout(3 ,2 ));
//PS:f.setBounds(200, 200, 400, 400);与f.pack()相互冲突,f.pack()根据内容自动包裹
综合的例子:
public class test1 {
public static void main (String[] args) {
Frame f = new Frame();
f.setBounds(200 , 200 , 400 , 400 );
f.setLayout(new GridLayout(2 , 1 ));
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 ));
p11.add(new Button("按钮" ));
p11.add(new Button("按钮" ));
p1.add(new Button("按钮" ), BorderLayout.EAST);
p1.add(new Button("按钮" ), BorderLayout.WEST);
p1.add(p11, BorderLayout.CENTER);
for (int i=0 ;i<4 ;i++){
p21.add(new Button("按钮" ));
}
p2.add(new Button("按钮" ), BorderLayout.EAST);
p2.add(new Button("按钮" ), BorderLayout.WEST);
p2.add(p21, BorderLayout.CENTER);
f.add(p1);
f.add(p2);
f.setVisible(true );
}
}
6.事件监听 实现ActionListener接口,并且重写actionPerformed方法 7.TextField JAVA中单引号是char双引号是string
public class test1 {
public static void main (String[] args) {
Frame f = new Frame();
TextFieldtf=new TextField();
tf.addActionListener(new MyListener());
tf.setEchoChar('*' );
f.add(tf);
f.pack();
f.setVisible(true );
}
}
classMyListener implements ActionListener{
@Override
public void actionPerformed (ActionEvent e) {
TextField t=(TextField)e.getSource();
System.out .println(t.getText());
}
}