- 项目目标:
- 实现一个简单的计算器 ,可以进行加、减、乘、除、三角函数、乘方、平方根及阶乘等运算,除此之外,还可以进行清屏,程序可以关闭等。
- 掌握Swing组件的应用、事件响应等
- 主要涉及以下知识点:
- Swing包、JFrame窗体
- FlowLayout布局方式
- GUI组件:JPanel、JLabel、JFrame、JTextField、JButton等
- 事件响应:ActionListener、ActionEvent等
- 计算器逻辑运算实现
- 需求说明:
- 1、图形界面的设计一般都不是唯一的,大家可以自由发挥,要求界面整洁、美观大方;页面布局可参考下图;
- 2、在本例中,所有的按钮都有事件响应,并且都使用addActionListener()方法注册了监听事件;
- 3、JiSuanQi类实现了ActionListener接口,重写了该接口中的actionPerformed(ActionEvent e)方法。这个方法中,根据e.getSource()的返回值判断单击的是哪个按钮对象,不同的按钮对象做相应的事件处理。
- 功能图:
package JAVA3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Objects;
//利用JFrame创建容器让Conter继承
//导入侦听器接口MouseListener
public class Conter extends JFrame implements MouseListener {
JFrame list;
// Container con;
JTextField show;
//创建模块并添加功能
JButton[] jbNum = new JButton[10];
JPanel jpMain; // 主面板
JPanel jpRight; // 右子面板主要用于存放运算符和等号
JPanel jpLight; // 左子面板用于存放数字,符号, “.”
JButton digit; // 小数点
JButton sign; // 正负号
JButton add; // 加号
JButton sub; // 减号
JButton multiply; // 乘号
JButton divide; // 除号
JButton power; // 求幂
JButton cos; // cos
JButton sin; // sin
JButton ln; // ln
JButton ce; // 清除
JButton equal; // 等于
JButton mod; // 取余
JButton sqrt; // sqrt
double sum = 0;
boolean b = false;
operator i = operator.un;
//创建enum类添加操作符
enum operator {
add, sub, mul, div, mod, pow, un
}
void play() {
//创建主窗口
list = new JFrame("计算器");
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//定义大小
list.setSize(450, 350);
list.setLocation(600, 500);
list.setBackground(Color.BLACK);//设计操作符颜色
list.setResizable(false);
list.setLayout(new FlowLayout(FlowLayout.CENTER));
show = new JTextField(31);
show.setHorizontalAlignment(JTextField.RIGHT);//设置文本向右对齐
show.setEditable(false);
list.add(show);
//设置板面布局
jpMain = new JPanel();
jpRight = new JPanel();
jpLight = new JPanel();
jpMain.setLayout(new GridLayout(1, 2));
jpRight.setLayout(new GridLayout(4, 3, 3, 3));
jpLight.setLayout(new GridLayout(4, 3, 3, 3));
list.add(jpMain);
jpMain.add(jpLight);
jpMain.add(jpRight);
//设置数字版面规定1~9
for (int i = 9; i >= 0; i--) {
jbNum[i] = new JButton(String.valueOf(i));
jbNum[i].setForeground(Color.BLUE);
jpLight.add(jbNum[i]);
jbNum[i].addMouseListener(this);
}
//设置按钮显示
add = new JButton("+");
sub = new JButton("-");
multiply = new JButton("*");
divide = new JButton("÷");
power = new JButton("x^y");
sin = new JButton("sin");
cos = new JButton("cos");
ln = new JButton("!");
ce = new JButton("清除");
equal = new JButton("=");
mod = new JButton("1/x");
sqrt = new JButton("sqrt");
jpRight.add(divide);
jpRight.add(sqrt);
jpRight.add(ln);
jpRight.add(multiply);
jpRight.add(sin);
jpRight.add(mod);
jpRight.add(sub);
jpRight.add(cos);
jpRight.add(ce);
jpRight.add(add);
jpRight.add(power);
jpRight.add(equal);
//设置监听器记录
digit = new JButton(".");
sign = new JButton("±");
jpLight.add(sign);
jpLight.add(digit);
add.addMouseListener(this);
sub.addMouseListener(this);
multiply.addMouseListener(this);
divide.addMouseListener(this);
power.addMouseListener(this);
sin.addMouseListener(this);
cos.addMouseListener(this);
ln.addMouseListener(this);
ce.addMouseListener(this);
equal.addMouseListener(this);
mod.addMouseListener(this);
sqrt.addMouseListener(this);
digit.addMouseListener(this);
sign.addMouseListener(this);
list.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
// 0~9的输入
if (e.getSource() == jbNum[0]) {
input(0, e);
}
if (e.getSource() == jbNum[1]) {
input(1, e);
}
if (e.getSource() == jbNum[2]) {
input(2, e);
}
if (e.getSource() == jbNum[3]) {
input(3, e);
}
if (e.getSource() == jbNum[4]) {
input(4, e);
}
if (e.getSource() == jbNum[5]) {
input(5, e);
}
if (e.getSource() == jbNum[6]) {
input(6, e);
}
if (e.getSource() == jbNum[7]) {
input(7, e);
}
if (e.getSource() == jbNum[8]) {
input(8, e);
}
if (e.getSource() == jbNum[9]) {
input(9, e);
}
// 小数点,正负号,CE,等号
if (e.getSource() == digit) {
if (show.getText().indexOf('.') == -1) {
show.setText(show.getText() + ".");
}
}
if (e.getSource() == sign) {
if (!show.getText().contains("-")) {
show.setText("-" + show.getText());
} else {
show.setText(show.getText().replace('-', '\0'));
}
}
if (e.getSource() == ce) {
show.setText("0");
sum = 0;
i = operator.un;
b = false;
}
outer:
if (e.getSource() == equal) {
try {
switch (i) {
case un:
break;
case null:
default:
if (i == operator.add) {
sum += Double.parseDouble(show.getText());
}
if (i == operator.sub) {
sum -= Double.parseDouble(show.getText());
}
if (i == operator.mul) {
sum *= Double.parseDouble(show.getText());
}
if (i == operator.div) {
if (Double.parseDouble(show.getText()) != 0) {
sum /= Double.parseDouble(show.getText());
} else {
show.setText("ERROR");
b = true;
sum = 0;
break outer;
}
}
if (i == operator.mod) {
sum %= Double.parseDouble(show.getText());
}
if (i == operator.pow) {
sum = Math.pow(sum, Double.parseDouble(show.getText()));
}
trimIn(sum);
break;
}
} catch (Exception ex) {
show.setText("ERROR");
}
sum = 0;
i = operator.un;
b = true;
}
if (e.getSource() == add) {
cal(i);
i = operator.add;
b = true;
}
if (e.getSource() == sub) {
cal(i);
i = operator.sub;
b = true;
}
if (e.getSource() == multiply) {
cal(i);
i = operator.mul;
b = true;
}
if (e.getSource() == divide) {
cal(i);
i = operator.div;
b = true;
}
if (e.getSource() == mod) {
cal(i);
i = operator.mod;
b = true;
}
if (e.getSource() == power) {
cal(i);
i = operator.pow;
b = true;
}
try {
if (!Objects.equals(show.getText(), "ERROR")) {
if (e.getSource() == sqrt) {
sum = Math.sqrt(Double.parseDouble(show.getText()));
trimIn(sum);
b = true;
}
if (e.getSource() == sin) {
sum = Math.sin(Double.parseDouble(show.getText()));
trimIn(sum);
b = true;
}
if (e.getSource() == cos) {
sum = Math.cos(Double.parseDouble(show.getText()));
trimIn(sum);
b = true;
}
if (e.getSource() == ln) {
sum = Math.log(Double.parseDouble(show.getText()));
trimIn(sum);
b = true;
}
}
} catch (Exception ex) {
show.setText("ERROR");
b = true;
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public void cal(operator i) {
try {
if (!Objects.equals(show.getText(), "ERROR")) {
if (i == operator.un) {
sum = Double.parseDouble(show.getText());
}
if (i == operator.add) {
sum += Double.parseDouble(show.getText());
trimIn(sum);
}
if (i == operator.sub) {
sum -= Double.parseDouble(show.getText());
trimIn(sum);
}
if (i == operator.mul) {
sum *= Double.parseDouble(show.getText());
trimIn(sum);
}
if (i == operator.div) {
if (Double.parseDouble(show.getText()) != 0) {
sum /= Double.parseDouble(show.getText());
trimIn(sum);
} else {
show.setText("ERROR");
sum = 0;
b = true;
i = operator.un;
}
}
if (i == operator.mod) {
sum %= Double.parseDouble(show.getText());
trimIn(sum);
}
//幂指函数
if (i == operator.pow) {
sum = Math.pow(sum, Double.parseDouble(show.getText()));
trimIn(sum);
}
}
} catch (Exception ex) {
show.setText("ERROR");
b = true;
}
}
public void input(int i, MouseEvent e) {
if (b) {
show.setText(String.valueOf(i));
b = false;
} else {
//判断0和.来清除整数时后面的点
if (show.getText().indexOf('0') == 0 && e.getSource() != digit) {
show.setText(String.valueOf(i));
} else {
show.setText(show.getText() + i);
}
}
}
public void trimIn(double sum) {
if (String.valueOf(sum).indexOf('.') != -1
&& String.valueOf(sum).endsWith("0")) {
show.setText((String.valueOf(sum).substring(0, String.valueOf(sum)
.indexOf('.'))));
} else if (Double.isNaN(sum)) {
show.setText("ERROR");
b = true;
i = operator.un;
} else if (Double.isInfinite(sum)) {
show.setText("ERROR");
b = true;
i = operator.un;
} else {
show.setText(String.valueOf(sum));
}
}
public static void main(String[] args) {
Conter c = new Conter();
c.play();
}
}