模拟计算器程序 设计一个程序来模拟一个简单的手持计算器。程序支持算术运算+、-、*、⼀、=、以及C(清除)

2024-11-24 06:05:31
推荐回答(1个)
回答1:

package calculator;

public class Calculator {
public static void main(String[] args){
//Calculator calculator = new Calculator();
CalculatorView gui = new CalculatorView();
gui.init();
gui.showCalculator();
}
}

package calculator;

/**
* 计算器后台模型类
*
*/

public class CalculatorModel {

/**
* result结果
*/
public static double result;

/** 构造方法 */
public CalculatorModel(){

}

/** get、set方法 */
public static double getResult() {
return result;
}

public static void setResult(double result) {
CalculatorModel.result = result;
}

/** 加法 */
public static double plus(double firstNum,double secondNum){
result = firstNum + secondNum;
return result;
}

/** 减法 */
public static double minus(double firstNum,double secondNum){
result = firstNum - secondNum;
return result;
}

/** 乘法 */
public static double multiply(double firstNum,double secondNum){
result = firstNum * secondNum;
return result;
}

/** 除法 */
public static double divide(double firstNum,double secondNum){
if(secondNum==0){
throw new ArithmeticException("除数不能为0!");
}
result = firstNum / secondNum;
return result;
}

/** 结果清零 */
public static void clear(){
result = 0;
}

/** 打印结果 */
public static String printResult(double result){
return Double.toString(result);
}

}

package calculator;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class CalculatorView{
JFrame win = new JFrame("计算器");
JTextArea textArea;
/** 第一个数 */
String firstNum = "";
/** 第二个数 */
String secondNum = "";
/** 运算符 */
String operation;

public void init(){

JPanel mainPanel = createBorderPanel();
JMenuBar menuBar = createMenuBar();
JPanel centerPanel = createBorderPanel();
JTextArea textArea = createTextArea();
JPanel keyPanel = createKeyPanel();

//组合panel
win.add(mainPanel);
mainPanel.add(BorderLayout.NORTH,menuBar);
mainPanel.add(BorderLayout.CENTER,centerPanel);
centerPanel.add(BorderLayout.NORTH,textArea);
centerPanel.add(BorderLayout.CENTER,keyPanel);

win.setSize(200,200);
win.setLocation(400, 300);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void showCalculator(){
win.setVisible(true);
}

/** 创建按钮面板 */
private JPanel createKeyPanel() {
JPanel keyPanel = new JPanel();
keyPanel.setLayout(new GridLayout(4,4));

JButton[] keyButton = new JButton[16];
char[] keyChar = "789+456-123*0.=/".toCharArray();

for(int i=0;i keyButton[i] = new JButton(Character.toString(keyChar[i]));
keyButton[i].addActionListener(new CalculatorController());
keyPanel.add(keyButton[i]);
}

return keyPanel;
}

/** 创建文本域 */
private JTextArea createTextArea() {
textArea = new JTextArea();
textArea.setEditable(false);
return textArea;
}

/** 创建主面板 */
private JPanel createBorderPanel() {
JPanel borderPanel = new JPanel();
borderPanel.setLayout(new BorderLayout());
return borderPanel;
}

/** 创建菜单栏 */
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();

JMenu editMenu = new JMenu("编辑");
JMenuItem copyMenuItem = new JMenuItem("复制");
JMenuItem parseMenuItem = new JMenuItem("粘贴");
editMenu.add(copyMenuItem);
editMenu.add(parseMenuItem);

JMenu lookMenu = new JMenu("查看");
JMenuItem stopMenuItem = new JMenuItem("停止");
JMenuItem refreshMenuItem = new JMenuItem("刷新");
JMenuItem clearMenuItem = new JMenuItem("清零");
clearMenuItem.addActionListener(new CalculatorController());
lookMenu.add(clearMenuItem);
lookMenu.add(stopMenuItem);
lookMenu.add(refreshMenuItem);

JMenu helpMenu = new JMenu("帮助");
JMenuItem helpMenuItem = new JMenuItem("帮助");
JMenuItem aboutMenuItem = new JMenuItem("关于");
helpMenu.add(helpMenuItem);
helpMenu.add(aboutMenuItem);

menuBar.add(editMenu);
menuBar.add(lookMenu);
menuBar.add(helpMenu);

return menuBar;
}

/** 内部类-控制器类 */
public class CalculatorController implements ActionListener{

public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("+")){

secondNum = firstNum;
operation = "+";
firstNum = "";
}else if(e.getActionCommand().equals("-")){
secondNum = firstNum;
operation = "-";
firstNum = "";
}else if(e.getActionCommand().equals("*")){
secondNum = firstNum;
operation = "*";
firstNum = "";
}else if(e.getActionCommand().equals("/")){
secondNum = firstNum;
operation = "/";
firstNum = "";
}else if(e.getActionCommand().equals(".")){
if(!firstNum.contains(".")){
firstNum += ".";
}
}else if(e.getActionCommand().equals("=")){
if(!operation.equals("")){
calculateEquals();
}

}else if(e.getActionCommand().equals("清零")){
clear();
}else{
firstNum += e.getActionCommand();
textArea.setText(firstNum);
}

}

public void clear(){
firstNum = "";
secondNum = "";
textArea.setText("0");
}

public void calculateEquals(){
double first = Double.valueOf(firstNum);
double second = Double.valueOf(secondNum);
double result = 0;

if(operation.equals("+")){
result = CalculatorModel.plus(second, first);
}else if(operation.equals("-")){
result = CalculatorModel.minus(second, first);
}else if(operation.equals("*")){
result = CalculatorModel.multiply(second, first);
}else if(operation.equals("/")){
try{
result = CalculatorModel.divide(second, first);
}catch(ArithmeticException e){
e.printStackTrace();
JOptionPane.showMessageDialog(win, e.getMessage());
}

}
firstNum = Double.toString(result);
operation = "";
textArea.setText(CalculatorModel.printResult(result));
}

}

}