-2

単純な計算機を作成するこれらの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メソッドであることがわかりますが、なぜですか?なぜこれが機能しないのか本当にわかりません:/

4

1 に答える 1

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 に数値以外を誰も入れていないことが確実にわかっている場合は、null 値のみをテストできます。

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;
    }
}

また、これらの数値の信頼できない情報源に依存しているため、優れたアーキテクチャに理想的なソリューションはありませんが、それによって、あなたが求めていた特定の問題を解決できるはずです。

優れたソリューションを得るには、ソリューション全体を再設計する必要がありますが、あなたの割り当てはそれを必要としないと思います。

于 2013-02-14T17:12:53.210 に答える