灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2909回复:0

java例程练习(简单的计算器[调停者设计模式及内部类])

楼主#
更多 发布于:2012-09-08 09:44

import java.awt.*;
import java.awt.event.*;

public class TestInnerClass {

    public static void main(String[] args) {
        new TFFrame().launchFrame();
    }

}

class TFFrame extends Frame {
    public void launchFrame() {
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(15);
        Label lbPlus = new Label("+");
        Button btnEqual = new Button("=");
        setLayout(new FlowLayout());
        add(num1);
        add(lbPlus);
        add(num2);
        add(btnEqual);
        add(num3);
        btnEqual.addActionListener(new MyMonitor(num1, num2, num3));
        pack();
        setVisible(true);
    }

}

class MyMonitor implements ActionListener {
    TextField num1,num2,num3;
    MyMonitor(TextField num1, TextField num2, TextField num3) {//用构造函数实现
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
    }


    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(num1.getText());
        int n2 = Integer.parseInt(num2.getText());
        int n3 = n1 + n2;
        num3.setText("" + n3);

    }
}
[java]
import java.awt.*;
import java.awt.event.*;

public class TestInnerClass {

    public static void main(String[] args) {
        new TFFrame().launchFrame();
    }

}

class TFFrame extends Frame {
    public void launchFrame() {
        TextField num1 = new TextField(10);
        TextField num2 = new TextField(10);
        TextField num3 = new TextField(15);
        Label lbPlus = new Label("+");
        Button btnEqual = new Button("=");
        setLayout(new FlowLayout());
        add(num1);
        add(lbPlus);
        add(num2);
        add(btnEqual);
        add(num3);
        btnEqual.addActionListener(new MyMonitor(this/*num1, num2, num3*/));
        pack();
        setVisible(true);
    }

}

class MyMonitor implements ActionListener {
//  TextField num1,num2,num3;
//  MyMonitor(TextField num1, TextField num2, TextField num3) {
//      this.num1 = num1;
//      this.num2 = num2;
//      this.num3 = num3;
//  }

     //持有对方的
    TFFrame tf = null;
    public MyMonitor(TFFrame tf) {
        this.tf = tf;
    }

    public void actionPerformed(ActionEvent e) {
        int n1 = Integer.parseInt(tf.num1.getText());
        int n2 = Integer.parseInt(tf.num2.getText());
        int n3 = n1 + n2;
        tf.num3.setText("" + n3);

    }
}
[java]
import java.awt.*;
import java.awt.event.*;

public class TestInnerClass {

    public static void main(String[] args) {
        new TFFrame().launchFrame();
    }

}

class TFFrame extends Frame {
    TextField num1, num2, num3;
    public void launchFrame() {
        num1 = new TextField(10);
        num2 = new TextField(10);
        num3 = new TextField(15);
        Label lbPlus = new Label("+");
        Button btnEqual = new Button("=");
        setLayout(new FlowLayout());
        add(num1);
        add(lbPlus);
        add(num2);
        add(btnEqual);
        add(num3);
        btnEqual.addActionListener(new MyMonitor());
        pack();
        setVisible(true);
    }

    //内部类:1,方便的访问包装类的成员
    //       2,该类不允许或不需要其他类进行访问
    class MyMonitor implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            int n3 = n1 + n2;
            num3.setText("" + n3);
        }
    }

}


摘自 Yours风之恋


喜欢0 评分0
游客

返回顶部