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

  • 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());        
        }
    }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DYanchao2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值