java GUI(二)

组合大于继承

class A extends B{

}//继承

class A {
	public B b;
}

计算器初级代码

class myCalculator extends  Frame{
    public myCalculator(){
        TextField num1 = new TextField("10");//max 10 strs
        TextField num2 = new TextField("10");//max 10 strs
        TextField resl = new TextField("20");//max 20 strs

        Button but1 = new Button("=");
        Label l1 = new Label("+");

        setLayout(new FlowLayout());
        add(num1);
        add(l1);
        add(num2);
        add(but1);
        add(resl);

        pack();
        setVisible(true);

       but1.addActionListener(new myCalculatorListener(num1,num2,resl));

    }
}

class myCalculatorListener implements ActionListener{
    private TextField num1;
    private TextField num2;
    private TextField res;
    public myCalculatorListener(TextField num1 ,TextField num2,TextField res) {
        this.num1= num1;
        this.num2= num2;
        this.res=res;//把传进来的参数传到定义的变量,下面就可以用
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 =Integer.parseInt(num1.getText());
        int n2 =Integer.parseInt(num2.getText());

        res.setText(""+(n1+n2));
        num1.setText("");
        num2.setText("");
        
    }
}

计算器修改代码(更加面型对象)

public class gui {
    public static void main(String[] args) {
        new myCalculator().loadFrame();
    }
}

class myCalculator extends  Frame{
    TextField num1,num2,resl;

    public void loadFrame(){
        num1 = new TextField("10");
        num2 = new TextField("10");
        resl = new TextField("20");
        Button but1 = new Button("=");
        Label l1 = new Label("+");

        but1.addActionListener(new myCalculatorListener(this));

        setLayout(new FlowLayout());
        add(num1);
        add(l1);
        add(num2);
        add(but1);
        add(resl);

        pack();
        setVisible(true);
    }
}

class myCalculatorListener implements ActionListener{

    myCalculator cal = null;
    public myCalculatorListener(myCalculator calculator){
        this.cal = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(cal.num1.getText());
        int n2 = Integer.parseInt(cal.num2.getText());
        cal.resl.setText(""+(n1+n2));
        cal.num1.setText("");
        cal.num2.setText("");
    }
}

完全改造为面向对象(内部类)

public class gui {
    public static void main(String[] args) {
        new myCalculator().loadFrame();
    }
}

class myCalculator extends  Frame{
    TextField num1,num2,resl;

    public void loadFrame(){
        num1 = new TextField("10");
        num2 = new TextField("10");
        resl = new TextField("20");
        Button but1 = new Button("=");
        Label l1 = new Label("+");

        but1.addActionListener(new myCalculatorListener());

        setLayout(new FlowLayout());
        add(num1);
        add(l1);
        add(num2);
        add(but1);
        add(resl);

        pack();
        setVisible(true);
    }
    
   private class myCalculatorListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            resl.setText(""+(n1+n2));
            num1.setText("");
            num2.setText("");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值