java高级语言 - GUI 02 简单计算器的实现 组合 内部类优化

简易计算器

组合大于继承

class A extends B{
  // inheritance
}
class A{
  public B b;
  // combination
  // refer!
}

code

package com.zepei;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalculator {
    public static void main(String[] args) {
        new Calculator();
    }
}

// calculator
class Calculator extends Frame{
    public Calculator(){
        // 3 textfields
        TextField num1 = new TextField(10);// number of characters
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(20);

        // 1 button
        Button button = new Button("=");
        button.addActionListener(new MyCalListener(num1,num2,num3));

        // 1 tag/label
        Label label = new Label("+");

        // layout
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
}

// listener class
class MyCalListener implements ActionListener {
    // get 3 variables
    private TextField num1,num2,num3;
    public MyCalListener(TextField num1,TextField num2,TextField num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // get add number and added number
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());

        // put the result into third text field
        num3.setText(""+(n1+n2));

        // clear the first two text field
        num1.setText("");
        num2.setText("");
    }
}

code optimization – combination

—> object-oriented

package com.zepei;

// optimize testCalculator using combination

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

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

// calculator
class Calculator1 extends Frame{
    // attribute
    TextField num1,num2,num3;
    // method
    public void loadFrame(){
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalListener1(this));
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }

// listener class
class MyCalListener1 implements ActionListener {
    // get calculator object
    Calculator1 calculator = null;
    public MyCalListener1(Calculator1 calculator) {
        this.calculator = calculator;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // get add number and added number
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());

        // put the result into third text field
        calculator.num3.setText(""+(n1+n2));

        // clear the first two text field
        calculator.num1.setText("");
        calculator.num2.setText("");

    }
}

code optimization2 – inner class

The biggest advantage of inner class is it can get outer attributes and methods with no obstruction.

// optimization using inner class

package com.zepei;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

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

// calculator
class Calculator2 extends Frame {
    // attribute
    TextField num1, num2, num3;

    // method
    public void loadFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(20);
        Button button = new Button("=");
        Label label = new Label("+");

        button.addActionListener(new MyCalListener2());
        setLayout(new FlowLayout());

        add(num1);
        add(label);
        add(num2);
        add(button);
        add(num3);

        pack();
        setVisible(true);
    }
    // listener class
    private class MyCalListener2 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            // get add number and added number
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());

            // put the result into third text field
            num3.setText(""+(n1+n2));

            // clear the first two text field
            num1.setText("");
            num2.setText("");
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值