• 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(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);
    
    	}
    }
    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);
    
    	}
    }
    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);
    		}
    	}
    
    }
    



  • 相关阅读:
    ASP.NET AJAX Beta 1 发布 (转载)
    ASP.NET里常用的JS (转贴)
    让您的Ajax应用加载数据时界面友好 (转贴)
    模态窗口 javascript html
    最亲密接触Dhtml&JScript开发细节 (转贴)
    Hashtable的使用
    2009年全国年节及纪念日放假办法
    详解.NET中的动态编译
    CSS2.0样式手册_说明_SDK下载chm
    [转]DISTINCT 和 ORDER BY 使用第三个字段进行排序
  • 原文地址:https://www.cnblogs.com/wjchang/p/3671616.html
Copyright © 2020-2023  润新知