理解面向对象编程(五)-GUI

来源:互联网 发布:武田毅雄 知乎 编辑:程序博客网 时间:2024/05/22 09:43
  • 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);//是否可见
  • 3.super()调用父类构造方法
class MyFrame extendsFrame{    MyFrame(){        //调用父类Frame的构造方法,因为Frame有两个构造方法,Frame()和Frame(String s),所以在此调用第二个,给new出来的MyFrame命名              super("MyFrame");    }} 
  • 4.Panel
    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);//相对于Frame    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());                }    }   
0 0
原创粉丝点击