2013-02-14 119 views
-2

我有这两个类来实现一个简单的计算器,但是当我尝试使用它我得到的错误java.lang.NumberFormatException,这里是源代码:为什么这个方法返回一个空字符串?

的SimpleCalc类:

//Imports are listed in full to show what's being used 
//could just import javax.swing.* and java.awt.* etc.. 

import java.awt.GridLayout; 
import java.awt.BorderLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.JButton; 
import java.awt.Container; 

public class SimpleCalc implements ActionListener{ 

    public static final SimpleCalc instance = new SimpleCalc(); 

    JFrame guiFrame; 
    JPanel buttonPanel; 
    JTextField numberCalc; 
    int calcOperation = 0; 
    int currentCalc; 
    int operatorAction; 

    //Note: Typically the main method will be in a 
    //separate class. As this is a simple one class 
    //example it's all in the one class. 


    public SimpleCalc() 
    { 
     guiFrame = new JFrame(); 

     //make sure the program exits when the frame closes 
     guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     guiFrame.setTitle("Simple Calculator"); 
     guiFrame.setSize(300,300); 

     //This will center the JFrame in the middle of the screen 
     guiFrame.setLocationRelativeTo(null); 

     numberCalc = new JTextField(); 
     numberCalc.setHorizontalAlignment(JTextField.RIGHT); 
     numberCalc.setEditable(false); 

     guiFrame.add(numberCalc, BorderLayout.NORTH); 

     buttonPanel = new JPanel(); 

     //Make a Grid that has three rows and four columns 
     buttonPanel.setLayout(new GridLayout(4,3)); 
     guiFrame.add(buttonPanel, BorderLayout.CENTER); 

     //Add the number buttons 
     for (int i=1;i<10;i++) 
     { 
      addButton(buttonPanel, String.valueOf(i)); 
     } 

     JButton addButton = new JButton("+"); 
     addButton.setActionCommand("+"); 

     OperatorAction subAction = new OperatorAction(1); 
     addButton.addActionListener(subAction); 

     JButton subButton = new JButton("-"); 
     subButton.setActionCommand("-"); 

     OperatorAction addAction = new OperatorAction(2); 
     subButton.addActionListener(addAction); 

     JButton equalsButton = new JButton("="); 
     equalsButton.setActionCommand("="); 
     equalsButton.addActionListener(new ActionListener() 
     { 
      @Override 
      public void actionPerformed(ActionEvent event) 
      { 
       if (!numberCalc.getText().isEmpty()) 
       { 
        int number = Integer.parseInt(numberCalc.getText()); 
        if (calcOperation == 1) 
        { 
         int calculate = currentCalc + number; 
         numberCalc.setText(Integer.toString(calculate)); 
        } 
        else if (calcOperation == 2) 
        { 
         int calculate = currentCalc - number; 
         numberCalc.setText(Integer.toString(calculate)); 
        } 
       } 
      } 
     }); 

     buttonPanel.add(addButton); 
     buttonPanel.add(subButton); 
     buttonPanel.add(equalsButton); 
     guiFrame.setVisible(true); 
    } 

    //All the buttons are following the same pattern 
    //so create them all in one place. 
    private void addButton(Container parent, String name) 
    { 
     JButton but = new JButton(name); 
     but.setActionCommand(name); 
     but.addActionListener(this); 
     parent.add(but); 
    } 

    //As all the buttons are doing the same thing it's 
    //easier to make the class implement the ActionListener 
    //interface and control the button clicks from one place 
    @Override 
    public void actionPerformed(ActionEvent event) 
    { 
     //get the Action Command text from the button 
     String action = event.getActionCommand(); 

     //set the text using the Action Command text 
     numberCalc.setText(action);  
    } 

    public String getText() { 
     return numberCalc.getText(); 
    } 

} 

这里是其它类OperatorAction:

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

import javax.swing.JTextField; 

    public class OperatorAction implements ActionListener 
    { 
     int calcOperation = 0; 
     int currentCalc; 
     private int operator; 
     // How to make 


     public OperatorAction(int operation) 
     { 
      operator = operation; 
     } 

     public void actionPerformed(ActionEvent event) 
     { 
      currentCalc = Integer.parseInt(SimpleCalc.instance.getText()); 
      calcOperation = operator; 
     } 
    } 

所以我做得到,这是我的getText方法失败,但是为什么呢?我真的不明白为什么这不应该工作:/

+0

你能发布确切的消息(其中包含你需要的所有细节)吗? – assylias 2013-02-14 16:35:33

+0

这应该被关闭为太本地化。 – djechlin 2013-02-14 16:36:02

+0

你需要找到一个比你的代码更普遍的问题。首先修剪所有与您的问题无关的代码,找到一个可以从中复制的简单例子,如果您没有答案,请编辑此问题或发布更密切的代码。 – djechlin 2013-02-14 16:37:23

回答

0

只要改变你的动作类这样的:

public class OperatorAction implements ActionListener 
{ 
    int calcOperation = 0; 
    int currentCalc; 
    private int operator; 
    // How to make 


    public OperatorAction(int operation) 
    { 
     operator = operation; 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     String text = SimpleCalc.instance.getText(); 
     try { 
      currentCalc = Integer.parseInt(text); 
     } catch (NumberFormatException e) { 
      currentCalc = 0; 
     } 
     calcOperation = operator; 
    } 
} 

另外,如果你肯定知道,没有人提出任何东西,除了数量SimpleCalc.instance,你可以只测试空值。

public class OperatorAction implements ActionListener 
{ 
    int calcOperation = 0; 
    int currentCalc; 
    private int operator; 
    // How to make 


    public OperatorAction(int operation) 
    { 
     operator = operation; 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     String text = SimpleCalc.instance.getText(); 
     currentCalc = text != null ? Integer.parseInt(text) : 0; 
     calcOperation = operator; 
    } 
} 

还记得,没有一个解决方案是理想的一个很好的架构,因为他们依赖于对这些数字信息不可靠的来源,但应该可以解决你询问具体问题。

对于一个好的解决方案,您需要重新设计整个解决方案,但我认为您的任务并不需要这样做。

相关问题