package project5;
import javax.swing.*;
import javax.xml.bind.annotation.XmlElementRef;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class CalculatorSelf extends JFrame implements ActionListener {
int sum = 0;
int preNumber = 0;
int tempMul = 1;
int tempDiv = 1;
int tempMod = 1;
int tempSum = 0;
//记录输入状态 点击数字按钮则为0,如果点击运算符按钮则转为1
short status = 0;
//存储前一个运算符 0:无运算符 1:+ 2:- 3:× 4:÷ 5:% 6:=
short preOperator = 0;
JTextField input, tempResult, BIN, OCT, HEX;
public CalculatorSelf() {
super("程序员计算器");
}
public void init() {
Container contentPane = getContentPane();
input = new JTextField();
tempResult = new JTextField();
input.setEditable(false);
tempResult.setEditable(false);
//用于存放显示框 输入框 2进制、8进制、16进制框
JPanel TransPanel = new JPanel(new GridLayout(5, 1));
BIN = new JTextField(50);
OCT = new JTextField(50);
HEX = new JTextField(50);
BIN.setEditable(false);
OCT.setEditable(false);
HEX.setEditable(false);
//文本框初始化
input.setHorizontalAlignment(JTextField.RIGHT);
tempResult.setHorizontalAlignment(JTextField.RIGHT);
BIN.setHorizontalAlignment(JTextField.RIGHT);
OCT.setHorizontalAlignment(JTextField.RIGHT);
HEX.setHorizontalAlignment(JTextField.RIGHT);
tempResult.setBackground(new Color(230, 230, 230));
input.setBackground(new Color(255, 255, 255));
BIN.setBackground(new Color(230, 230, 230));
OCT.setBackground(new Color(230, 230, 230));
HEX.setBackground(new Color(230, 230, 230));
TransPanel.add(tempResult);
TransPanel.add(input);
TransPanel.add(BIN);
TransPanel.add(OCT);
TransPanel.add(HEX);
//按钮初始化
Font font = new Font("Times New Roman", Font.BOLD, 16);
String[] button = {"<<", ">>", "AC", "DEL",
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "×",
"+/-", "0", "%", "÷"};
JPanel buttonPanelUp = new JPanel(new GridLayout(5, 4));
for (int i = 0; i < 4; i++) {
JButton temp = new JButton(button[i]);
temp.setFocusPainted(false);
buttonPanelUp.add(temp);
temp.addActionListener(this);
temp.addMouseListener(new MyMouseListener1());
temp.setFont(font);
temp.setBackground(new Color(240, 240, 240));
}
//7、8、9按钮初始化
for (int i = 4; i < 7; i++) {
JButton temp = new JButton(button[i]);
temp.setFocusPainted(false);
buttonPanelUp.add(temp);
temp.addActionListener(this);
temp.addMouseListener(new MyMouseListener2());
temp.setFont(font);
temp.setBackground(new Color(250, 250, 250));
}
//+ 按钮初始化
JButton addButton = new JButton(button[7]);
addButton.setFocusPainted(false);
buttonPanelUp.add(addButton);
addButton.addActionListener(this);
addButton.addMouseListener(new MyMouseListener1());
addButton.setFont(font);
addButton.setBackground(new Color(240, 240, 240));
//4、5、6按钮初始化
for (int i = 8; i < 11; i++) {
JButton temp = new JButton(button[i]);
temp.setFocusPainted(false);
buttonPanelUp.add(temp);
temp.addActionListener(this);
temp.addMouseListener(new MyMouseListener2());
temp.setFont(font);
temp.setBackground(new Color(250, 250, 250));
}
//- 按钮初始化
JButton subButton = new JButton(button[11]);
subButton.setFocusPainted(false);
buttonPanelUp.add(subButton);
subButton.addActionListener(this);
subButton.addMouseListener(new MyMouseListener1());
subButton.setFont(font);
subButton.setBackground(new Color(240, 240, 240));
//1、2、3按钮初始话
for (int i = 12; i < 15; i++) {
JButton temp = new JButton(button[i]);
temp.setFocusPainted(false);
buttonPanelUp.add(temp);
temp.addActionListener(this);
temp.addMouseListener(new MyMouseListener2());
temp.setFont(font);
temp.setBackground(new Color(250, 250, 250));
}
//× 按钮初始化
JButton mulButton = new JButton(button[15]);
mulButton.setFocusPainted(false);
buttonPanelUp.add(mulButton);
mulButton.addActionListener(this);
mulButton.addMouseListener(new MyMouseListener1());
mulButton.setFont(font);
mulButton.setBackground(new Color(240, 240, 240));
//+/- 按钮初始化
JButton sigButton = new JButton(button[16]);
sigButton.setFocusPainted(false);
buttonPanelUp.add(sigButton);
sigButton.addActionListener(this);
sigButton.addMouseListener(new MyMouseListener1());
sigButton.setFont(font);
sigButton.setBackground(new Color(240, 240, 240));
//0按钮初始化
JButton zeroButton = new JButton(button[17]);
zeroButton.setFocusPainted(false);
buttonPanelUp.add(zeroButton);
zeroButton.addActionListener(this);
zeroButton.addMouseListener(new MyMouseListener2());
zeroButton.setFont(font);
zeroButton.setBackground(new Color(250, 250, 250));
//%、= 按钮初始化
for (int i = 18; i < 20; i++) {
JButton temp = new JButton(button[i]);
temp.setFocusPainted(false);
buttonPanelUp.add(temp);
temp.addActionListener(this);
temp.addMouseListener(new MyMouseListener1());
temp.setFont(font);
temp.setBackground(new Color(240, 240, 240));
}
//buttonPanel.add(buttonPanelUp);
JButton equalButton = new JButton("=");
equalButton.setFocusPainted(false);
equalButton.addActionListener(this);
equalButton.addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e) {
equalButton.setBackground(new Color(72,156,222));
}
@Override
public void mouseExited(MouseEvent e) {
equalButton.setBackground(new Color(138,186,224));
}
});
equalButton.setFont(font);
equalButton.setBackground(new Color(138, 186, 224));
//buttonPanel.add(equalButton);
contentPane.add(TransPanel);
contentPane.add(buttonPanelUp);
contentPane.add(equalButton);
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
Dimension d = contentPane.getSize();
TransPanel.setBounds(0,0,d.width,d.height/5);
buttonPanelUp.setBounds(0,TransPanel.getHeight(),d.width,d.height*2/3);
equalButton.setBounds(0,TransPanel.getHeight()+buttonPanelUp.getHeight(),d.width,contentPane.getHeight()-TransPanel.getHeight()-buttonPanelUp.getHeight());
}
});
//JFrame初始化设置
URL resource = CalculatorSelf.class.getResource("IconImage.png");
ImageIcon imageIcon = new ImageIcon(resource);
this.setIconImage(imageIcon.getImage());
this.setVisible(true);
this.setBounds(700, 300, 350, 500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//this.setResizable(false);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (Character.isDigit(cmd.charAt(0))) {
if(status == 0) {
//这里做了一个�
![avatar](https://profile-avatar.csdnimg.cn/5ef93472aa40412dba9002db99f0399b_2301_78772942.jpg!1)
程序猿小D
- 粉丝: 4427
- 资源: 1584
最新资源
- 【ELM回归预测】RELM鲁棒极限学习机数据预测【含Matlab源码 3050期】.zip
- 【LSTM时序预测】LSTM时间序列神经网络预测【含Matlab源码 2267期】.zip
- 【covid 时间序列】冠状病毒病例、死亡、疫苗接种仿真【含GUI Matlab源码 2262期】.zip
- 【RNN数据预测】时间反向传播 (BPTT) 训练RNN递归神经网络预测【含Matlab源码 2434期】.zip
- 【轨迹预测】卡尔曼滤波运动轨迹预测【含Matlab源码 590期】.zip
- 【价格预测】粒子群算法黄金价格预测【含Matlab源码 591期】.zip
- 【时间序列预测】最小均方(LMS)算法时间序列预测【含Matlab源码 1335期】.zip
- 【数据生成】SNN浅层神经网络数据生成【含Matlab源码 7371期】.zip
- 【时间序列预测】RBF神经网络时间序列预测【含Matlab源码 1336期】.zip
- 基于预设性能约束的航天器编队姿态控制技术研究:事件触发机制下的跟踪控制策略,预设性能约束下的航天器编队事件触发姿态协同跟踪控制策略,预设性能约束下的航天器编队事件触发姿态跟踪控制 预设性能控制 编队控
- 【CNN回归预测】卷积神经网络CNN数据回归预测【含Matlab源码 2003期】.zip
- 【LSTM回归预测】LSTM神经网络回归预测【含Matlab源码 2227期】.zip
- 【GMDH预测】GMDH时间序列预测【含Matlab源码 2189期】.zip
- 【ORELM回归预测】离群鲁棒极限学习机ORELM回归预测【含Matlab源码 1441期】.zip
- 【LSTM回归预测】布谷鸟算法优化LSTM回归预测【含Matlab源码 2037期】.zip
- 【LSTM回归预测】主成分分析结合BiLSTM数据回归预测【含Matlab源码 2276期】.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)