import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame {
// 输入框,显示当前输入的数字或结果
private JTextField textField;
// 数字0-9及小数点按钮
private JButton button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonDot;
// 加减乘除按钮
private JButton buttonPlus, buttonMinus, buttonMultiply, buttonDivide;
// 操作(等于、清空、删除)按钮
private JButton buttonEqual, buttonClear, buttonDelete;
// 平方、立方、乘方、n次方、开平方、开n次方、倒数按钮
private JButton buttonSquare, buttonCube, buttonPower, buttonNthPow, buttonRoot, buttonNthRoot, buttonReciprocal;
// 三角函数、反三角函数按钮
private JButton buttonSin, buttonCos, buttonTan, buttonAsin, buttonAcos, buttonAtan;
// 当前操作
private Operation currentOperation;
// 当前值
private int currentValue;
// 枚举操作类型
private enum Operation {
ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, POWER, NTHROOT
}
public Calculator() {
initComponents();
initListeners();
}
/**
* 初始化组件
*/
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculator");
// 使用GridBagLayout布局方式
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
// 初始化输入框
textField = new JTextField();
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 6; // 扩展6列
constraints.fill = GridBagConstraints.BOTH;
add(textField, constraints);
// 初始化数字、小数点按钮
button7 = new JButton("7");
button8 = new JButton("8");
button9 = new JButton("9");
button4 = new JButton("4");
button5 = new JButton("5");
button6 = new JButton("6");
button1 = new JButton("1");
button2 = new JButton("2");
button3 = new JButton("3");
button0 = new JButton("0");
buttonDot = new JButton(".");
addButton(constraints, button7, 0, 1);
addButton(constraints, button8, 1, 1);
addButton(constraints, button9, 2, 1);
addButton(constraints, button4, 0, 2);
addButton(constraints, button5, 1, 2);
addButton(constraints, button6, 2, 2);
addButton(constraints, button1, 0, 3);
addButton(constraints, button2, 1, 3);
addButton(constraints, button3, 2, 3);
addButton(constraints, button0, 0, 4);
addButton(constraints, buttonDot, 1, 4);
// 初始化平方、立方、乘方、n次方、开平方、开n次方、倒数按钮
buttonSquare = new JButton("x²");
buttonCube = new JButton("x³");
buttonPower = new JButton("x^y");
buttonNthPow = new JButton("x^n");
buttonRoot = new JButton("√x");
buttonNthRoot = new JButton("n√x");
buttonReciprocal = new JButton("1/x");
addButton(constraints, buttonSquare, 3, 1);
addButton(constraints, buttonCube, 4, 1);
addButton(constraints, buttonPower, 5, 1);
addButton(constraints, buttonNthPow, 3, 2);
addButton(constraints, buttonRoot, 4, 2);
addButton(constraints, buttonNthRoot, 5, 2);
addButton(constraints, buttonReciprocal, 5, 3);
// 初始化加减乘除按钮
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMultiply = new JButton("×");
buttonDivide = new JButton("÷");
addButton(constraints, buttonPlus, 3, 3);
addButton(constraints, buttonMinus, 4, 3);
addButton(constraints, buttonMultiply, 3, 4);
addButton(constraints, buttonDivide, 4, 4);
// 初始化操作(等于、清空、删除)按钮
buttonEqual = new JButton("=");
buttonClear = new JButton("C");
buttonDelete = new JButton("DEL");
constraints.gridwidth = 2;
addButton(constraints, buttonEqual, 5, 4);
constraints.gridx = 3;
addButton(constraints, buttonClear, 0, 5);
constraints.gridx = 4;
addButton(constraints, buttonDelete, 1, 5);
// 初始化三角函数、反三角函数按钮
buttonSin = new JButton("sin");
buttonCos = new JButton("cos");
buttonTan = new JButton("tan");
buttonAsin = new JButton("asin");
buttonAcos = new JButton("acos");
buttonAtan = new JButton("atan");
addButton(constraints, buttonSin, 0, 6);
addButton(constraints, buttonCos, 1, 6);
addButton(constraints, buttonTan, 2, 6);
addButton(constraints, buttonAsin, 3, 6);
addButton(constraints, buttonAcos, 4, 6);
addButton(constraints, buttonAtan, 5, 6);
pack();
}
/**
* 添加按钮
*/
private void addButton(GridBagConstraints constraints, JButton button, int x, int y) {
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = 1;
add(button, constraints);
}
/**
* 初始化各个按钮的监听器
*/
private void initListeners() {
// 数字0-9、小数点按钮监听器
ActionListener numberListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String textValue = textField.getText();
textValue += ((JButton)e.getSource()).getText();
textField.setText(textValue);
}
};
button0.addActionListener(numberListener);
button1.addActionListener(numberListener);
button2.addActionListener(numberListener);
button3.addActionListener(numberListener);
button4.addActionListener(numberListener);
button5.addActionListener(numberListener);
button6.addActionListener(numberListener);
button7.addActionListener(numberListener);
button8.addActionListener(numberListener);
button9.addActionListener(numberListener);
buttonDot.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!textField.getText().contains(".")) {
String textValue = textField.getText();
textValue += ".";
textField.setText(textValue);
}
}
});
// 平方、立方、乘方、n次方、开平方、开n次方、倒数按钮监听器
ActionListener operationListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double value = Double.parseDouble(textField.getText());
switch (((JButton)e.getSource()).getText()) {
case "x²":
textField.setText(Double.toString(Math.pow(value, 2)));
break;
case "x³":
textField.setText(Double.toString(Math.pow(value, 3)));
break;
case "x^y":
currentValue = (int)Math.round(value);
currentOperation = Operation.POWER;
textField.setText("");
break;
case "x^n":
currentValue = (int)Math.round(value);
currentOperation = Operation.NTHROOT;
textField.setText("");
break;
case "n√x":
currentValue = (int)Math.round(value);
currentOperation = Operation.NTHROOT
没有合适的资源?快使用搜索试试~ 我知道了~
基于JavaEE技术的全栈开发学习资源汇总
共2000个文件
js:797个
html:596个
css:244个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 178 浏览量
2024-10-09
04:35:18
上传
评论
收藏 64.73MB ZIP 举报
温馨提示
本资源包为JavaEE全栈开发学习平台,包含3222个文件,涵盖835个JavaScript文件、681个HTML文件、354个Java类文件、258个CSS样式文件、215个XML配置文件、95个PNG图片文件、64个SCSS样式文件、55个Git忽略文件和34个Idea项目配置文件。它整合了多种编程语言,旨在帮助开发者全面掌握JavaEE全栈开发技能。
资源推荐
资源详情
资源评论
收起资源包目录
基于JavaEE技术的全栈开发学习资源汇总 (2000个子文件)
bootstrap.min.css 115KB
bootstrap.min.css 115KB
bootstrap.min.css 115KB
editormd.css 76KB
editormd.css 76KB
editormd.css 76KB
editormd.min.css 60KB
editormd.min.css 60KB
editormd.min.css 60KB
editormd.preview.css 55KB
editormd.preview.css 55KB
editormd.preview.css 55KB
editormd.preview.min.css 44KB
editormd.preview.min.css 44KB
editormd.preview.min.css 44KB
ambiance.css 26KB
ambiance.css 26KB
ambiance.css 26KB
ambiance.css 26KB
bootstrap-theme.min.css 19KB
bootstrap-theme.min.css 19KB
bootstrap-theme.min.css 19KB
codemirror.css 8KB
codemirror.css 8KB
codemirror.css 8KB
codemirror.css 8KB
jsdoc-default.css 6KB
jsdoc-default.css 6KB
jsdoc-default.css 6KB
codemirror.min.css 5KB
codemirror.min.css 5KB
codemirror.min.css 5KB
codemirror.min.css 5KB
mdn-like.css 5KB
mdn-like.css 5KB
mdn-like.css 5KB
mdn-like.css 5KB
solarized.css 5KB
solarized.css 5KB
solarized.css 5KB
solarized.css 5KB
qunit-1.16.0.css 5KB
qunit-1.16.0.css 5KB
qunit-1.16.0.css 5KB
merge.css 3KB
merge.css 3KB
merge.css 3KB
merge.css 3KB
lint.css 3KB
lint.css 3KB
lint.css 3KB
lint.css 3KB
xq-dark.css 3KB
xq-dark.css 3KB
xq-dark.css 3KB
xq-dark.css 3KB
common.css 2KB
common.css 2KB
common.css 2KB
lesser-dark.css 2KB
lesser-dark.css 2KB
lesser-dark.css 2KB
lesser-dark.css 2KB
pastel-on-dark.css 2KB
pastel-on-dark.css 2KB
pastel-on-dark.css 2KB
pastel-on-dark.css 2KB
conmmon.css 2KB
xq-light.css 2KB
xq-light.css 2KB
xq-light.css 2KB
xq-light.css 2KB
list.css 2KB
tomorrow-night-eighties.css 2KB
tomorrow-night-eighties.css 2KB
tomorrow-night-eighties.css 2KB
tomorrow-night-eighties.css 2KB
editormd.logo.css 2KB
editormd.logo.css 2KB
editormd.logo.css 2KB
erlang-dark.css 2KB
erlang-dark.css 2KB
erlang-dark.css 2KB
erlang-dark.css 2KB
prettify-tomorrow.css 2KB
prettify-tomorrow.css 2KB
prettify-tomorrow.css 2KB
zenburn.css 2KB
zenburn.css 2KB
zenburn.css 2KB
zenburn.css 2KB
twilight.css 2KB
twilight.css 2KB
twilight.css 2KB
twilight.css 2KB
midnight.css 2KB
midnight.css 2KB
midnight.css 2KB
midnight.css 2KB
vibrant-ink.css 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
csbysj2020
- 粉丝: 2536
- 资源: 5471
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功