volcano 1 year ago
parent c70a79b85f
commit 0821635da0

@ -0,0 +1,165 @@
import javax.swing.*;
import java.awt.*;
public class CalculatorWindow extends JFrame {
Computer computer;
JButton numberButtons[];
JButton operatorButton[];
//加减乘除按钮
JButton dot,fu,back,equation,clear,sqrt,reciprocal;
JTextField resultShow;
//显示计算结果
JTextField showTempResult;
//临时结果
JLabel showOperator;
//运算符
JTextField showDataItem;
//当前参与运算的数据项
JTextArea computerProcess;
//计算步骤
JButton saveBtn,copyBtn,clearTextBtn;
public CalculatorWindow() throws HeadlessException {
computer=new Computer();
initView();//设置界面
initActionListener();//注册监视器
}
public void initView(){
this.setTitle("计算器");
JPanel left,right;
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.LEFT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesRoman",Font.BOLD,14));
resultShow.setEditable(false);
resultShow.setBackground(Color.lightGray);
showTempResult=new JTextField();
showTempResult.setHorizontalAlignment(JTextField.RIGHT);
showTempResult.setFont(new Font("Arial",Font.BOLD,14));
showTempResult.setEditable(false);
showOperator=new JLabel();
showOperator.setHorizontalAlignment(JTextField.CENTER);
showOperator.setFont(new Font("Arial",Font.BOLD,18));
showDataItem=new JTextField();
showDataItem.setHorizontalAlignment(JTextField.LEFT);
showDataItem.setBackground(Color.white);
showDataItem.setFont(new Font("Arial",Font.BOLD,14));
showDataItem.setEditable(false);
computerProcess=new JTextArea();
computerProcess.setEditable(false);
computerProcess.setFont(new Font("宋体",Font.PLAIN,14));
numberButtons=new JButton[10];
for (int i = 0; i <=9; i++) {
numberButtons[i]=new JButton(" "+i);
numberButtons[i].setFont(new Font("Arial",Font.BOLD,20));
}
operatorButton=new JButton[4];
String s[]={"+","-","*","/"};
for (int i = 0; i < 4; i++) {
operatorButton[i]=new JButton(s[i]);
operatorButton[i].setFont(new Font("Arial",Font.BOLD,20));
}
dot=new JButton(".");
fu=new JButton("+/-");
equation=new JButton("=");
back=new JButton("退格");
clear=new JButton("C");
sqrt=new JButton("sqrt");
reciprocal=new JButton("1/x");
saveBtn=new JButton("保存");
copyBtn=new JButton("复制");
clearTextBtn=new JButton("清除");
left=new JPanel();
right=new JPanel();
//左边布局
left.setLayout(new BorderLayout());
//BorderLayout 容器分为五个部分(东南西北中),每部分只能包含一个组件
JPanel centerInLeft=new JPanel();
Box box=Box.createHorizontalBox();
//创建水平排列组件的Box
box.add(showTempResult);
box.add(showOperator);
box.add(showDataItem);
left.add(box,BorderLayout.NORTH);
left.add(centerInLeft,BorderLayout.CENTER);
centerInLeft.setLayout(new GridLayout(5,5));
//GridLayout 网格布局5行5列
centerInLeft.add(numberButtons[1]);
centerInLeft.add(numberButtons[2]);
centerInLeft.add(numberButtons[3]);
centerInLeft.add(operatorButton[0]);
centerInLeft.add(back);
centerInLeft.add(numberButtons[4]);
centerInLeft.add(numberButtons[5]);
centerInLeft.add(numberButtons[6]);
centerInLeft.add(operatorButton[1]);
centerInLeft.add(clear);
centerInLeft.add(numberButtons[7]);
centerInLeft.add(numberButtons[8]);
centerInLeft.add(numberButtons[9]);
centerInLeft.add(operatorButton[2]);
centerInLeft.add(reciprocal);
centerInLeft.add(numberButtons[0]);
centerInLeft.add(fu);
centerInLeft.add(dot);
centerInLeft.add(operatorButton[3]);
centerInLeft.add(sqrt);
for (int i = 0; i < 4; i++) {
centerInLeft.add(new JLabel());
}
centerInLeft.add(equation);
//右边布局
right.setLayout(new BorderLayout());
right.add(resultShow,BorderLayout.NORTH);
right.add(new JScrollPane(computerProcess),BorderLayout.CENTER);
JPanel southInRight=new JPanel();
southInRight.add(saveBtn);
southInRight.add(copyBtn);
southInRight.add(clearTextBtn);
right.add(southInRight,BorderLayout.SOUTH);
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);
//split 左右布局的分割面板
add(split,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置 点击关闭退出程序
setVisible(true);
setBounds(200,200,788,258);//设置位置大小
validate();
}
public void initActionListener(){
HandleDight hd=new HandleDight(this);
for (int i = 0; i <= 9; i++) {
numberButtons[i].addActionListener(hd);
}
dot.addActionListener(hd);
HandleOperator ho=new HandleOperator(this);
for (int i = 0; i < 4; i++) {
operatorButton[i].addActionListener(ho);
}
fu.addActionListener(new HandleFu(this));
sqrt.addActionListener(new HandleSqrt(this));
reciprocal.addActionListener(new HandleReciprocal(this));
back.addActionListener(new HandleBack(this));
equation.addActionListener(new HandleEquation(this));
clear.addActionListener(new HandleClear(this));
HandleFile hf=new HandleFile(this);
saveBtn.addActionListener(hf);
copyBtn.addActionListener(hf);
clearTextBtn.addActionListener(hf);
}
}

@ -0,0 +1,129 @@
import java.util.Stack;
public class Computer {
public double result;//结果
public Stack<Character> dataItem;//参与运算数据项
public Stack<Double> tempResult;//中间结果
public Stack<Character> operator;//运算符号
public Computer(){
init();
}
public void init(){//初始化
dataItem=new Stack<Character>();
tempResult=new Stack<Double>();
dataItem.push('0');
operator=new Stack<Character>();
}
public double getResult() {
double endItem=0;
if(dataItem.empty()){
endItem=tempResult.peek();
}else{
endItem=computerDataItem();
}
if(operator.empty()){
result=endItem;
return result;
}
char c=operator.peek();
if (c == '+') {
result=tempResult.peek()+endItem;
}else if (c == '-') {
result=tempResult.peek()-endItem;
}else if (c == '*') {
result=tempResult.peek()*endItem;
}else if (c == '/') {
result=tempResult.peek()/endItem;
}
return result;
}
public void setDataItem(char c) {
//数据项中不包含小数点时才可以添加小数点
if(c!='.')
dataItem.push(c);
else {
if(!dataItem.contains('.'))
dataItem.push(c);
}
}
public double getTempResult() {
double t=0;
if(tempResult.empty()){
t=computerDataItem();
}else{
t=tempResult.peek();
}
return t;
}
public char getOperator() {
if(operator.empty()){
return '\0';
}
return operator.peek();
}
public void setOperator(char p) {
if(dataItem.empty()){
operator.clear();//防止重复输入运算符
operator.push(p);//保留用户最后确定的运算符
return;
}
if(operator.empty()){
operator.push(p);
double m=computerDataItem();//计算数据项
dataItem.removeAllElements();//清空数据项中的数字
tempResult.push(m);//临时结果压入tempResult
}else{
double m1=computerDataItem();
dataItem.removeAllElements();
char c= operator.pop();//弹出已有的运算符
double t=tempResult.pop();//弹出临时结果
if (c == '+') {
t=t+m1;
}else if (c == '-') {
t=t-m1;
}else if (c == '*') {
t=t*m1;
}else if (c == '/') {
t=t/m1;
}
tempResult.push(t);//把新的结果压入tempResult
operator.push(p);//把新的运算符压入operator
}
}
public double computerDataItem(){//计算出数据项的double型结果
if(dataItem.empty()){
return tempResult.peek();
}
StringBuffer sb=new StringBuffer();
double doubleData=0;
for (int i = 0; i < dataItem.size(); i++) {
sb.append(dataItem.get(i));//获取堆栈中的数字但不弹栈
}
try {
doubleData=Double.parseDouble(sb.toString());
}catch (NumberFormatException e){
doubleData=0;
}
return doubleData;
}
public void backspace(){
if(dataItem.size()>=1){
dataItem.pop();
}
}
public void initMath(MathComputer mc){
mc.handle(this);
}
}
Loading…
Cancel
Save