简单计算器
需求分析
输入第一个和第二个数,点击等号按钮,得到正确答案,并清空前两个按钮。
最基础的代码:
package com.queryor.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
public Calculator(){
//三个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
//给Button添加监听
button.addActionListener(new myCalculatorListener(num1,num2,num3));
//一个标签
Label label = new Label("+");
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class myCalculatorListener implements ActionListener{
private TextField num1,num2,num3;
//获取三个变量
public myCalculatorListener(TextField num1,TextField num2,TextField num3) {
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.计算值并放到第三个框
num3.setText(""+(n1+n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
面向对象的写法
构造监听器类时从传参数编程了传对象
package com.queryor.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
public Calculator(){
//三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
//给Button添加监听
//一个标签
Label label = new Label("+");
button.addActionListener(new myCalculatorListener(this));
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器类
class myCalculatorListener implements ActionListener{
//获取计算器对象
Calculator calculator = null;
public myCalculatorListener(Calculator calculator) {
this.calculator = calculator;
}
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(calculator.num1.getText());
int n2 = Integer.parseInt(calculator.num2.getText());
//2.计算值并放到第三个框
calculator.num3.setText(""+(n1+n2));
//3.清除前两个框
calculator.num1.setText("");
calculator.num2.setText("");
}
}
内部类写法
- 更好的包装
内部类可以很容易获取外部类的属性。
package com.queryor.lesson02;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//简易计算器
public class TestCalc {
public static void main(String[] args) {
new Calculator();
}
}
//计算器类
class Calculator extends Frame{
//属性
TextField num1,num2,num3;
public Calculator(){
//三个文本框
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(10);
//一个按钮
Button button = new Button("=");
//给Button添加监听
//一个标签
Label label = new Label("+");
button.addActionListener(new myCalculatorListener());
//布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
//监听器类
private class myCalculatorListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//1.获得加数和被加数
int n1 = Integer.parseInt(num1.getText());
int n2 = Integer.parseInt(num2.getText());
//2.计算值并放到第三个框
num3.setText(""+(n1+n2));
//3.清除前两个框
num1.setText("");
num2.setText("");
}
}
}