2016-12-29 58 views
-3

我有两个不同的类。是的,他们在同一个项目中,包装一切。这个项目来自Yakov Fain的一本书:Java Programming for Kids,Parents,and Grandparents。页数:95-100。您可以轻松获得该书的PDF。下面是两个不同类别的名称。为了我将展示我的两个班。Java中的计算器,不工作

类是计算器本身:在运行计算器计算器

类:CalculatorEngine

import javax.swing.*; 
import java.awt.BorderLayout; 
import java.awt.GridLayout; 

public class Calculator { 

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 buttonPoint = new JButton("."); 
JButton buttonEqual = new JButton("="); 
JButton buttonPlus = new JButton("+"); 
JButton buttonSub = new JButton("-"); 
JButton buttonMult = new JButton("*"); 
JButton buttonDiv = new JButton("/"); 
JPanel windowContent = new JPanel(); 
JTextField displayField = new JTextField(30); 

Calculator() { 

    BorderLayout bl = new BorderLayout(); 
    windowContent.setLayout(bl); 

    windowContent.add("North", displayField); 

    JPanel p1 = new JPanel(); 
    GridLayout gl = new GridLayout(4, 3); 
    p1.setLayout(gl); 

    p1.add(button0); 
    p1.add(button1); 
    p1.add(button2); 
    p1.add(button3); 
    p1.add(button4); 
    p1.add(button5); 
    p1.add(button6); 
    p1.add(button7); 
    p1.add(button8); 
    p1.add(button9); 
    p1.add(buttonPoint); 
    p1.add(buttonEqual); 

    windowContent.add("Center", p1); 

    JPanel p2 = new JPanel(); 
    GridLayout gl2 = new GridLayout(4, 1); 
    p2.setLayout(gl2); 
    p2.add(buttonDiv); 
    p2.add(buttonMult); 
    p2.add(buttonSub); 
    p2.add(buttonPlus); 

    windowContent.add("East", p2); 

    JFrame frame = new JFrame("Calculator"); 
    frame.setContentPane(windowContent); 

    frame.pack(); 
    frame.setVisible(true); 

    CalculatorEngine calcEngine = new CalculatorEngine(this); 

    button0.addActionListener(calcEngine); 
    button1.addActionListener(calcEngine); 
    button2.addActionListener(calcEngine); 
    button3.addActionListener(calcEngine); 
    button4.addActionListener(calcEngine); 
    button5.addActionListener(calcEngine); 
    button6.addActionListener(calcEngine); 
    button7.addActionListener(calcEngine); 
    button8.addActionListener(calcEngine); 
    button9.addActionListener(calcEngine); 
    buttonDiv.addActionListener(calcEngine); 
    buttonMult.addActionListener(calcEngine); 
    buttonSub.addActionListener(calcEngine); 
    buttonPlus.addActionListener(calcEngine); 
    buttonPoint.addActionListener(calcEngine); 
    buttonEqual.addActionListener(calcEngine); 
} 

public static void main(String[] args) { 
    Calculator calc = new Calculator(); 
} 
} 

*这是Calculator类。

**下面是运行Calculator类的代码。

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 

public class CalculatorEngine implements ActionListener { 

Calculator parent; 
char selectedAction = ' '; 

double currentResult = 0; 

CalculatorEngine(Calculator parent) { 
    this.parent = parent; 
} 

public void actionPerformed(ActionEvent e) { 

    JButton clickedButton = (JButton) e.getSource(); 
    String dispFieldText = parent.displayField.getText(); 

    double displayValue = 0; 

    if (!"".equals(dispFieldText)) { 
     displayValue = Double.parseDouble(dispFieldText); 
    } 
    Object src = e.getSource(); 

    if (src == parent.buttonPlus) { 
     selectedAction = '+'; 
     currentResult = displayValue; 
     parent.displayField.setText(""); 
    } else if (src == parent.buttonSub) { 
     selectedAction = '-'; 
     currentResult = displayValue; 
     parent.displayField.setText(""); 
    } else if (src == parent.buttonMult) { 
     selectedAction = '*'; 
     currentResult = displayValue; 
     parent.displayField.setText(""); 
    } else if (src == parent.buttonDiv) { 
     selectedAction = '/'; 
     currentResult = displayValue; 
     parent.displayField.setText(""); 

     if (selectedAction == '+') { 
      currentResult += displayValue; 
      parent.displayField.setText("" + currentResult); 
     } else if (selectedAction == '-') { 
      currentResult -= displayValue; 
      parent.displayField.setText("" + currentResult); 
     } else if (selectedAction == '*') { 
      currentResult *= displayValue; 
      parent.displayField.setText("" + currentResult); 
     } else if (selectedAction == '/') { 
      currentResult /= displayValue; 
      parent.displayField.setText("" + currentResult); 
     } 
    } else { 
     String clickedButtonLabel = clickedButton.getText(); 
     parent.displayField.setText(dispFieldText + clickedButtonLabel); 
    } 
} 
} 

这两个类显示计算器到控制台,看起来是这样的: (https://i.stack.imgur.com/Y4XHE.png

每当我运行该程序,我把一个等式,例如:1 + 1,然后我按'=',程序不会运行,并且在这种情况下不会给出等式2的结果。我也查看了书中的代码,它是相同的,或者我认为它是相同的。

+0

你可能想把eclipse拿掉标题 – SaggingRufus

+0

好吧,会的。谢谢! –

+0

范围界定问题。你只在处理'/'的块中执行计算。 – EJP

回答

0

您忘记处理用户单击actionPerformed方法中的等号(=)按钮(在CalculatorEngine.java内)的事件。改变它到这个,它的工作原理:

public void actionPerformed(ActionEvent e) { 

     JButton clickedButton = (JButton) e.getSource(); 
     String dispFieldText = parent.displayField.getText(); 

     double displayValue = 0; 

     if (!"".equals(dispFieldText)) { 
      displayValue = Double.parseDouble(dispFieldText); 
     } 
     Object src = e.getSource(); 

     if (src == parent.buttonPlus) { 
      selectedAction = '+'; 
      currentResult = displayValue; 
      parent.displayField.setText(""); 
     } else if (src == parent.buttonSub) { 
      selectedAction = '-'; 
      currentResult = displayValue; 
      parent.displayField.setText(""); 
     } else if (src == parent.buttonMult) { 
      selectedAction = '*'; 
      currentResult = displayValue; 
      parent.displayField.setText(""); 
     } else if (src == parent.buttonDiv) { 
      selectedAction = '/'; 
      currentResult = displayValue; 
      parent.displayField.setText(""); 
     } else if (src == parent.buttonEqual) { 
      if (selectedAction == '+') { 
       currentResult += displayValue; 
       parent.displayField.setText("" + currentResult); 
      } else if (selectedAction == '-') { 
       currentResult -= displayValue; 
       parent.displayField.setText("" + currentResult); 
      } else if (selectedAction == '*') { 
       currentResult *= displayValue; 
       parent.displayField.setText("" + currentResult); 
      } else if (selectedAction == '/') { 
       currentResult /= displayValue; 
       parent.displayField.setText("" + currentResult); 
      } 
     } else { 
      String clickedButtonLabel = clickedButton.getText(); 
      parent.displayField.setText(dispFieldText + clickedButtonLabel); 
     } 
} 
+0

我已经用上面发布的代码替换了原来的代码。它工作,我很感激。你一直在帮助很大,感谢@smoggers。 –