package _18;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private JPanel centerPanel = new JPanel();
private String x = ""; // 第一个操作数
private String y = ""; // 第二个操作数
private String fh = ""; // 运算符
private double answer; // 计算结果
private JTextField tfAnswer; // 结果显示框
// 构造方法:初始化界面
public Calculator() {
this.setBackground(Color.lightGray);
setLayout(new BorderLayout());
// 初始化显示文本框(右对齐,初始值0.)
tfAnswer = new JTextField();
tfAnswer.setHorizontalAlignment(JTextField.RIGHT);
tfAnswer.setText("0.");
add(tfAnswer, BorderLayout.NORTH);
// 中间按钮面板:5x5网格布局
centerPanel.setLayout(new GridLayout(5, 5));
// 添加所有按钮
addButton("7", Color.blue);
addButton("8", Color.blue);
addButton("9", Color.blue);
addButton("/", Color.red);
addButton("C", Color.red);
addButton("4", Color.blue);
addButton("5", Color.blue);
addButton("6", Color.blue);
addButton("*", Color.red);
addButton("^", Color.blue);
addButton("1", Color.blue);
addButton("2", Color.blue);
addButton("3", Color.blue);
addButton("-", Color.red);
addButton("sqrt", Color.blue);
addButton("0", Color.blue);
addButton("+/-", Color.blue);
addButton(".", Color.blue);
addButton("+", Color.red);
addButton("=", Color.red);
addButton("sin", Color.red);
addButton("cos", Color.red);
addButton("tan", Color.red);
addButton("ln", Color.red);
// 补充5x5布局的最后一个按钮(示例为“log”)
addButton("log", Color.red);
add(centerPanel, BorderLayout.CENTER);
}
// 自定义方法:添加按钮到面板
public void addButton(String name, Color color) {
JButton bt = new JButton(name);
bt.setBackground(Color.white);
bt.setForeground(color);
bt.addActionListener(this);
centerPanel.add(bt);
}
// 计算功能实现
public void dengyu(String z) {
if (z.equals("+")) {
answer = Double.parseDouble(x) + Double.parseDouble(y);
}
if (z.equals("-")) {
answer = Double.parseDouble(x) - Double.parseDouble(y);
}
if (z.equals("*")) {
answer = Double.parseDouble(x) * Double.parseDouble(y);
}
if (z.equals("/")) {
answer = Double.parseDouble(x) / Double.parseDouble(y);
}
if (z.equals("^")) {
answer = Math.pow(Double.parseDouble(x), Double.parseDouble(y));
}
if (z.equals("sin")) {
answer = Math.sin(Double.parseDouble(x));
}
if (z.equals("cos")) {
answer = Math.cos(Double.parseDouble(x));
}
if (z.equals("tan")) {
answer = Math.tan(Double.parseDouble(x));
}
if (z.equals("ln")) {
answer = Math.log(Double.parseDouble(x));
}
x = Double.toString(answer);
tfAnswer.setText(x);
y = "";
fh = "";
}
// 事件处理方法
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
// 数字按钮(0-9)
if (cmd.equals("0") || cmd.equals("1") || cmd.equals("2") || cmd.equals("3") || cmd.equals("4")
|| cmd.equals("5") || cmd.equals("6") || cmd.equals("7") || cmd.equals("8") || cmd.equals("9")) {
if (fh.equals("")) { // 未输入运算符,拼接第一个数
x += cmd;
if (x.startsWith("00")) { // 处理前导零
x = x.substring(1);
}
tfAnswer.setText(x);
} else { // 已输入运算符,拼接第二个数
y += cmd;
if (y.startsWith("00")) {
y = y.substring(1);
}
tfAnswer.setText(y);
}
}
// 小数点按钮
if (cmd.equals(".")) {
if (fh.equals("")) { // 第一个数加小数点
int j = 0;
for (int i = 0; i < x.length(); i++) {
if (x.charAt(i) == '.') {
j++;
}
}
if (j == 0) {
x += ".";
tfAnswer.setText(x);
}
} else { // 第二个数加小数点
int j = 0;
for (int i = 0; i < y.length(); i++) {
if (y.charAt(i) == '.') {
j++;
}
}
if (j == 0) {
y += ".";
tfAnswer.setText(y);
}
}
}
// 清空按钮(C)
if (cmd.equals("C")) {
x = "";
y = "";
fh = "";
tfAnswer.setText("0.");
}
// 正负切换按钮(+/-)
if (cmd.equals("+/-")) {
if (fh.equals("")) { // 切换第一个数的正负
if (x.startsWith("-")) {
x = x.substring(1);
} else {
x = "-" + x;
}
tfAnswer.setText(x);
} else { // 切换第二个数的正负
if (y.startsWith("-")) {
y = y.substring(1);
} else {
y = "-" + y;
}
tfAnswer.setText(y);
}
}
// 平方根按钮(sqrt)
if (cmd.equals("sqrt")) {
if (!fh.equals("")) {
dengyu(fh);
}
answer = Math.sqrt(Double.parseDouble(x));
x = Double.toString(answer);
tfAnswer.setText(x);
}
// 运算符按钮(+、-、*、/、^)
if (cmd.equals("+")) {
if (!fh.equals("")) {
dengyu(fh);
}
fh = "+";
}
if (cmd.equals("-")) {
if (!fh.equals("")) {
dengyu(fh);
}
fh = "-";
}
if (cmd.equals("*")) {
if (!fh.equals("")) {
dengyu(fh);
}
fh = "*";
}
if (cmd.equals("/")) {
if (!fh.equals("")) {
dengyu(fh);
}
fh = "/";
}
if (cmd.equals("^")) {
if (!fh.equals("")) {
dengyu(fh);
}
fh = "^";
}
// 三角函数/对数按钮(sin、cos、tan、ln)
if (cmd.equals("sin")) {
if (!fh.equals("")) {
dengyu(fh);
}
answer = Math.sin(Double.parseDouble(x));
x = Double.toString(answer);
tfAnswer.setText(x);
}
if (cmd.equals("cos")) {
if (!fh.equals("")) {
dengyu(fh);
}
answer = Math.cos(Double.parseDouble(x));
x = Double.toString(answer);
tfAnswer.setText(x);
}
if (cmd.equals("tan")) {
if (!fh.equals("")) {
dengyu(fh);
}
answer = Math.tan(Double.parseDouble(x));
x = Double.toString(answer);
tfAnswer.setText(x);
}
if (cmd.equals("ln")) {
if (!fh.equals("")) {
dengyu(fh);
}
answer = Math.log(Double.parseDouble(x));
x = Double.toString(answer);
tfAnswer.setText(x);
}
// 等于按钮(=)
if (cmd.equals("=")) {
dengyu(fh);
}
}
// 主方法:启动程序
public static void main(String args[]) {
Calculator c = new Calculator();
c.setTitle("Calculator");
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setSize(400, 400);
c.setVisible(true);
}
}
1336

被折叠的 条评论
为什么被折叠?



