📄 mycalculator.java
字号:
//MyCalculator.java
import java.awt.*; //包含用于创建用户界面和绘制图形图像的所有类
import java.awt.event.*; //提供处理由 AWT 组件所激发的各类事件的接口和类
import javax.swing.*;
//ActionListener用于接收操作事件的侦听器接口
public class MyCalculator extends JFrame implements ActionListener {
MyCalculator frame;
JTextField answer = new JTextField("0.");
JPanel panel = new JPanel();
JPanel pane2 = new JPanel();
String left = "";
String right = "";
String mid = "";
char flag = '\0';
boolean pit = true;
Container container = getContentPane();
// pane2上的控制按钮
JButton clear = new JButton("清零");
JButton button0 = new JButton("0");
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JButton button4 = new JButton("4");
JButton button5 = new JButton("5");
JButton button6 = new JButton("6");
JButton button7 = new JButton("7");
JButton button8 = new JButton("8");
JButton button9 = new JButton("9");
JButton sum = new JButton("+");
JButton sub = new JButton("-");
JButton mul = new JButton("×");
JButton div = new JButton("÷");
JButton point = new JButton(".");
JButton equal = new JButton("=");
JButton cos = new JButton("cos()");
JButton sin = new JButton("sin()");
JButton sqr = new JButton("sqrt");
JButton back = new JButton("退格");
public MyCalculator() { // 构造函数
super("简易计算器");
panel.setLayout(new BorderLayout()); // panel 的布局方式
pane2.setLayout(new GridLayout(5, 4,3,3)); // pane2 的布局方式
clear.addActionListener(this);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button0.addActionListener(this);
sum.addActionListener(this);
sub.addActionListener(this);
point.addActionListener(this);
equal.addActionListener(this);
cos.addActionListener(this);
sin.addActionListener(this);
div.addActionListener(this);
mul.addActionListener(this);
back.addActionListener(this);
sqr.addActionListener(this);
pane2.add(cos);
pane2.add(sin);
pane2.add(sqr);
pane2.add(back);
pane2.add(button1);
pane2.add(button2);
pane2.add(button3);
pane2.add(sum);
pane2.add(button4);
pane2.add(button5);
pane2.add(button6);
pane2.add(sub);
pane2.add(button7);
pane2.add(button8);
pane2.add(button9);
pane2.add(mul);
pane2.add(button0);
pane2.add(point);
pane2.add(equal);
pane2.add(div);
answer.setHorizontalAlignment(JTextField.RIGHT); // 设置文本的水平对齐方式
answer.setColumns(2); // 设置此 TextField 中的列数,然后验证布局
// 设置指定的 boolean 变量,以指示此 TextComponent 是否应该为可编辑
answer.setEditable(false);
answer.setBackground(Color.LIGHT_GRAY);
Font ft = new Font(null, Font.PLAIN, 20); //根据指定名称、样式和磅值大小,创建一个新 Font
answer.setFont(ft); // 设置当前字体
panel.add(clear, BorderLayout.NORTH);
panel.add(pane2, BorderLayout.CENTER);
container.add(answer, BorderLayout.NORTH);
container.add(panel, BorderLayout.CENTER);
WindowListener wl = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
MyCalculator.this.dispose();
System.exit(0);
}
};
this.addWindowListener(wl);
this.setSize(300, 280);
//this.setBackground(Color.red);
this.setVisible(true);
}
// 进行算术的运算
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) { // getSource()返回最初发生 Event 的对象
mid = mid + "1";
answer.setText(mid); // setText()将此 TextComponent 文本设置为指定文本
}
if (e.getSource() == button2) {
mid = mid + "2";
answer.setText(mid);
}
if (e.getSource() == button3) {
mid = mid + "3";
answer.setText(mid);
}
if (e.getSource() == button4) {
mid = mid + "4";
answer.setText(mid);
}
if (e.getSource() == button5) {
mid = mid + "5";
answer.setText(mid);
}
if (e.getSource() == button6) {
mid = mid + "6";
answer.setText(mid);
}
if (e.getSource() == button7) {
mid = mid + "7";
answer.setText(mid);
}
if (e.getSource() == button8) {
mid = mid + "8";
answer.setText(mid);
}
if (e.getSource() == button9) {
mid = mid + "9";
answer.setText(mid);
}
if (e.getSource() == button0) {
mid = mid + "0";
answer.setText(mid);
}
if (e.getSource() == point) { // 实现 “.”
String chk = answer.getText();
if (!chk.equals("0.")) {
if (pit && answer.getText().length() != 0) {
mid = mid + ".";
answer.setText(mid);
pit = false;
}
}
}
if (e.getSource() == back) { //
if (answer.getText().length() != 0) {
int len = answer.getText().length();
mid = answer.getText().substring(0, len - 1);
answer.setText(mid);
}
int g = mid.indexOf(".");
if (g == -1) {
pit = true;
}
}
if (e.getSource() == clear) {
mid = "";
answer.setText("0.");
}
if (e.getSource() == sum) {
flag = '+';
pit = true;
left = answer.getText();
mid = "";
answer.setText("");
}
if (e.getSource() == sub) {
String chk = answer.getText().trim();
if (chk.equals("") || chk.equals("0.")) {
mid = mid + "-";
answer.setText(mid);
} else {
flag = '-';
pit = true;
left = answer.getText();
mid = "";
answer.setText("");
}
}
if (e.getSource() == mul) {
flag = '×';
pit = true;
left = answer.getText();
mid = "";
answer.setText("");
}
if (e.getSource() == div) {
flag = '÷';
pit = true;
left = answer.getText();
mid = "";
answer.setText("");
}
try {
if (e.getSource() == sqr) {
double res1 = 0;
if (!answer.getText().equals("")) {
double sq = Double.parseDouble(answer.getText());
if (sq >= 0) {
res1 = Math.sqrt(sq);
answer.setText(String.valueOf(res1));
} else {
answer.setText("0");
}
}
}
if (e.getSource() == cos) {
if (!answer.getText().equals("")) {
double res2 = Math
.cos(Double.parseDouble(answer.getText()));
answer.setText(String.valueOf(res2));
}
}
if (e.getSource() == sin) {
if (!answer.getText().equals("")) {
double res2 = Math
.sin(Double.parseDouble(answer.getText()));
answer.setText(String.valueOf(res2));
}
}
} catch (NumberFormatException ex) {
System.out.println("出现了对非数字计算问题!");
}
if (e.getSource() == equal) {
double res = 0;
mid = "";
if (!left.equals("") && !answer.getText().equals("")) {
try {
switch (flag) {
case '+':
right = answer.getText();
res = Double.parseDouble(left)
+ Double.parseDouble(right);
answer.setText(String.valueOf(res));
break;
case '-':
right = answer.getText();
res = Double.parseDouble(left)
- Double.parseDouble(right);
answer.setText(String.valueOf(res));
break;
case '×':
right = answer.getText();
res = Double.parseDouble(left)
* Double.parseDouble(right);
answer.setText(String.valueOf(res));
break;
case '÷':
right = answer.getText();
if (Double.parseDouble(right) != 0) {
res = Double.parseDouble(left)
/ Double.parseDouble(right);
answer.setText(String.valueOf(res));
} else {
JOptionPane.showMessageDialog(null, "除数不能为零,请改正!",
"错误提示", JOptionPane.INFORMATION_MESSAGE);
left = "";
}
break;
}
} catch (NumberFormatException ex) {
System.out.println("出现了输入的异常");
} catch (RuntimeException ex) {
System.out.println("运行过程中出现异常");
}
}
}
}
public static void main(String args[]) {
MyCalculator application = new MyCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -