使用Java Swing窗口程序开发一个计算器项目源码,可以作为课程设计参考,喜欢的同学拿去使用研究吧
完整代码如下
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JTextArea;
class CalFrame extends Frame {
double d1, d2, c;
int op = 0;
TextField tf;
CalPanel p;
CalBackPanel p1;
CalFrame() {
super("计算器");
setLayout(new FlowLayout(1, 8, 20));// 流式布局器,使用指定的对齐方式及指定的垂直问距、水平间距创建
setBackground(Color.white);
setForeground(Color.black);// 设置前景颜色
setResizable(false);// 设置为用户不可调整窗体大小
setSize(400, 300);
setLocation(600, 250);
tf = new TextField(32);
tf.setEditable(true);// 设置为能编辑
tf.setBackground(Color.white);
tf.setForeground(Color.black);
tf.setFont(new Font("Arial", Font.BOLD, 16));// 设置字体
add(tf);
p = new CalPanel();
add(p);
p1 = new CalBackPanel();
add(p1);
setVisible(true);
addWindowListener(new Wclose());
}
class CalButton extends Button {
CalButton(String s) {
super(s);
setBackground(Color.white);
}
}
class CalPanel extends Panel {
CalButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bPN, bPoint;
CalButton bSqrt, bSin, bCos, bYx, bAdd, bSub, bMul, bDiv, bInt, bEqual, bCE, bRec, bAbs;
CalPanel() {
setLayout(new GridLayout(5, 5));
setFont(new Font("TimesRoman", Font.BOLD, 25));
b0 = new CalButton("0");
b1 = new CalButton("1");
b2 = new CalButton("2");
b3 = new CalButton("3");
b4 = new CalButton("4");
b5 = new CalButton("5");
b6 = new CalButton("6");
b7 = new CalButton("7");
b8 = new CalButton("8");
b9 = new CalButton("9");
bPN = new CalButton("+/-");
bPoint = new CalButton(".");
bDiv = new CalButton("/");
bAdd = new CalButton("+");
bSub = new CalButton("-");
bMul = new CalButton("*");
bSqrt = new CalButton("√");
bSin = new CalButton("sin");
bCos = new CalButton("cos");
bYx = new CalButton("y^x");
bInt = new CalButton("int");
bEqual = new CalButton("=");
bCE = new CalButton("CE");
bRec = new CalButton("1/x");
bAbs = new CalButton("| |");
add(bAdd);
bAdd.addActionListener(new BAdd());
add(bSub);
bSub.addActionListener(new BSubtraction());
add(bMul);
bMul.addActionListener(new BMultiplication());
add(bDiv);
bDiv.addActionListener(new BDivision());
add(bCE);
bCE.addActionListener(new BCE());
add(b7);
b7.addActionListener(new InputB7());
add(b8);
b8.addActionListener(new InputB8());
add(b9);
b9.addActionListener(new InputB9());
add(bSqrt);
bSqrt.addActionListener(new BSqrt());
add(bAbs);
bAbs.addActionListener(new BAbs());
add(b4);
b4.addActionListener(new InputB4());
add(b5);
b5.addActionListener(new InputB5());
add(b6);
b6.addActionListener(new InputB6());
add(bInt);
bInt.addActionListener(new BInt());
add(bSin);
bSin.addActionListener(new BSin());
add(b1);
b1.addActionListener(new InputB1());
add(b2);
b2.addActionListener(new InputB2());
add(b3);
b3.addActionListener(new InputB3());
add(bRec);
bRec.addActionListener(new BReciprocal());
add(bCos);
bCos.addActionListener(new BCos());
add(b0);
b0.addActionListener(new InputB0());
add(bPN);
bPN.addActionListener(new InputBPN());
add(bPoint);
bPoint.addActionListener(new InputBPoint());
add(bYx);
bYx.addActionListener(new BYx());
add(bEqual);
bEqual.addActionListener(new BEqual());
}
}
class CalBackPanel extends Panel {
CalButton bBack;
CalBackPanel() {
setLayout(new FlowLayout());
bBack = new CalButton("Backspace");
add(bBack);
bBack.addActionListener(new BBack());
}
}
// 输入7
class InputB7 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "7");
}
}
//输入8
class InputB8 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "8");
}
}
//输入9
class InputB9 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "9");
}
}
//输入4
class InputB4 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "4");
}
}
//输入5
class InputB5 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "5");
}
}
//输入6
class InputB6 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "6");
}
}
//输入1
class InputB1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "1");
}
}
//输入2
class InputB2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "2");
}
}
//输入3
class InputB3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "3");
}
}
//输入0
class InputB0 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
tf.setText(num + "0");
}
}
//取相反数
class InputBPN implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
double x1 = Double.parseDouble(str.substring(str.indexOf("=") + 1));
if (x1 > 0) {
tf.setText("-" + x1);
} else if (x1 < 0) {
tf.setText(String.valueOf(x1).substring(1));
} else {
tf.setText(x1 + "");
}
} else {
if (!tf.getText().equals("")) {
if (Double.parseDouble(tf.getText()) > 0) {
tf.setText("-" + tf.getText());
} else if (Double.parseDouble(tf.getText()) < 0) {
tf.setText(tf.getText().substring(1));
} else {
tf.setText(tf.getText());
}
} else {
tf.setText("您没有输入数字!");
}
}
}
}
//加小数点
class InputBPoint implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String num = tf.getText();
if (num.lastIndexOf(".") == -1) {
tf.setText(num + ".");
} else {
if (num.indexOf("+") != -1 || num.indexOf("-") != -1 || num.indexOf("*") != -1 || num.indexOf("/") != -1
|| num.indexOf("^") != -1) {
tf.setText(num + ".");
} else {
tf.setText("已有小数点");
}
}
}
}
//除法
class BDivision implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
if (str.indexOf("sin") != -1 || str.indexOf("cos") != -1 || str.indexOf("sqrt") != -1
|| str.indexOf("1/") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 4;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "/");
} else {
String str1 = str.substring(str.indexOf("=") + 1);
if (Double.parseDouble(str1.substring(str1.indexOf(".") + 1)) != 0) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 4;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "/");
} else {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 4;
tf.setText(tf.getText().substring(str.indexOf("=") + 1, str.indexOf(".")) + "/");
}
}
} else {
d1 = Double.parseDouble(tf.getText());
op = 4;
tf.setText(tf.getText() + "/");
}
}
}
//乘法
class BMultiplication implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
if (str.indexOf("sin") != -1 || str.indexOf("cos") != -1 || str.indexOf("sqrt") != -1
|| str.indexOf("1/") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 3;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "*");
} else {
String str1 = str.substring(str.indexOf("=") + 1);
if (Double.parseDouble(str1.substring(str1.indexOf(".") + 1)) != 0) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 3;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "*");
} else {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 3;
tf.setText(tf.getText().substring(str.indexOf("=") + 1, str.indexOf(".")) + "*");
}
}
} else {
d1 = Double.parseDouble(tf.getText());
op = 3;
tf.setText(tf.getText() + "*");
}
}
}
//减法
class BSubtraction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
if (str.indexOf("sin") != -1 || str.indexOf("cos") != -1 || str.indexOf("sqrt") != -1
|| str.indexOf("1/") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 2;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "-");
} else {
String str1 = str.substring(str.indexOf("=") + 1);
if (Double.parseDouble(str1.substring(str1.indexOf(".") + 1)) != 0) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 2;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "-");
} else {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 2;
tf.setText(tf.getText().substring(str.indexOf("=") + 1, str.indexOf(".")) + "-");
}
}
} else {
d1 = Double.parseDouble(tf.getText());
op = 2;
tf.setText(tf.getText() + "-");
}
}
}
//加法
class BAdd implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
if (str.indexOf("sin") != -1 || str.indexOf("cos") != -1 || str.indexOf("sqrt") != -1
|| str.indexOf("1/") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 1;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "+");
} else {
String str1 = str.substring(str.indexOf("=") + 1);
if (Double.parseDouble(str1.substring(str1.indexOf(".") + 1)) != 0) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 1;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "+");
} else {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 1;
tf.setText(tf.getText().substring(str.indexOf("=") + 1, str.indexOf(".")) + "+");
}
}
} else {
d1 = Double.parseDouble(tf.getText());
op = 1;
tf.setText(tf.getText() + "+");
}
}
}
//开根号
class BSqrt implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = Math.sqrt(d1);
tf.setText("sqrt(" + tf.getText().substring(str.indexOf("=") + 1) + ")" + "=" + d2);
} else {
d1 = Double.parseDouble(tf.getText());
d2 = Math.sqrt(d1);
tf.setText("sqrt(" + tf.getText() + ")" + "=" + d2);
}
}
}
//取整
class BInt implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
if (str.indexOf(".") != -1) {
String num = String.valueOf(d1);
String num1 = num.substring(num.indexOf("."));
char d3 = num1.charAt(1);
if (d3 >= '5') {
d2 = Math.ceil(d1);
} else {
d2 = Math.floor(d1);
}
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "=" + d2);
} else {
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "=" + String.valueOf(d1));
}
} else {
d1 = Double.parseDouble(tf.getText());
if (str.indexOf(".") != -1) {
String num = str.substring(str.indexOf("."));
char d3 = num.charAt(1);
if (d3 >= '5') {
d2 = Math.ceil(d1);
} else {
d2 = Math.floor(d1);
}
tf.setText(str + "=" + d2);
} else {
tf.setText(str + "=" + String.valueOf(d1));
}
}
}
}
//正弦函数
class BSin implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = Math.sin(d1);
tf.setText("sin" + tf.getText().substring(str.indexOf("=") + 1, str.indexOf(".")) + "=" + d2);
} else {
d1 = Double.parseDouble(tf.getText());
d2 = Math.sin(d1);
tf.setText("sin" + tf.getText() + "=" + d2);
}
}
}
//余弦函数
class BCos implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = Math.cos(d1);
tf.setText("cos" + tf.getText().substring(str.indexOf("=") + 1) + "=" + d2);
} else {
d1 = Double.parseDouble(tf.getText());
d2 = Math.cos(d1);
tf.setText("cos" + tf.getText() + "=" + d2);
}
}
}
//清除
class BCE implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
tf.setText("");
}
}
//幂运算
class BYx implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
if (str.indexOf("sin") != -1 || str.indexOf("cos") != -1 || str.indexOf("sqrt") != -1
|| str.indexOf("1/") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 5;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "^");
} else {
String str1 = str.substring(str.indexOf("=") + 1);
if (Double.parseDouble(str1.substring(str1.indexOf(".") + 1)) != 0) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 5;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "^");
} else {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
op = 5;
tf.setText(tf.getText().substring(str.indexOf("=") + 1) + "^");
}
}
} else {
d1 = Double.parseDouble(tf.getText());
op = 5;
tf.setText(tf.getText() + "^");
}
}
}
//取到数
class BReciprocal implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = 1 / d1;
tf.setText("1/" + tf.getText().substring(str.indexOf("=") + 1) + "=" + d2);
} else {
d1 = Double.parseDouble(tf.getText());
d2 = 1 / d1;
tf.setText("1/" + tf.getText() + "=" + d2);
}
}
}
//等于
class BEqual implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
double result = 0;
String str1 = tf.getText();
switch (op) {
case 1:
String str2 = str1.substring(str1.indexOf("+") + 1);
d2 = Double.parseDouble(str2);
result = d1 + d2;
break;
case 2:
String str10 = str1.substring(1);
String str8 = str10.substring(str10.indexOf("-") + 1);
d2 = Double.parseDouble(str8);
result = d1 - d2;
break;
case 3:
String str4 = str1.substring(str1.indexOf("*") + 1);
d2 = Double.parseDouble(str4);
result = d1 * d2;
break;
case 4:
String str5 = str1.substring(str1.indexOf("/") + 1);
d2 = Double.parseDouble(str5);
result = d1 / d2;
break;
case 5:
String str6 = str1.substring(str1.indexOf("^") + 1);
d2 = Double.parseDouble(str6);
result = Math.pow(d1, d2);
break;
default:
break;
}
tf.setText(str1 + "=" + String.valueOf(result));
}
}
//取绝对值
class BAbs implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
if (str.indexOf("=") != -1) {
if (str.indexOf("sin") != -1 || str.indexOf("cos") != -1 || str.indexOf("sqrt") != -1
|| str.indexOf("1/") != -1) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = Math.abs(d1);
tf.setText("|" + tf.getText().substring(str.indexOf("=") + 1) + "|" + "=" + d2);
} else {
String str1 = str.substring(str.indexOf("=") + 1);
if (Double.parseDouble(str1.substring(str1.indexOf(".") + 1)) != 0) {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = Math.abs(d1);
tf.setText("|" + tf.getText().substring(str.indexOf("=") + 1) + "|" + "=" + d2);
} else {
d1 = Double.parseDouble(tf.getText().substring(str.indexOf("=") + 1));
d2 = Math.abs(d1);
tf.setText(
"|" + tf.getText().substring(str.indexOf("=") + 1, str.indexOf(".")) + "|" + "=" + d2);
}
}
} else {
d1 = Double.parseDouble(tf.getText());
d2 = Math.abs(d1);
tf.setText("|" + tf.getText() + "|" + "=" + d2);
}
}
}
//退格
class BBack implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String str = tf.getText();
tf.setText(str.substring(0, str.length() - 1));
}
}
class Wclose extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
运行项目,电脑桌面会弹出计算器窗口程序
© 版权声明
本站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。敬请谅解!
THE END