簡體   English   中英

為什么不能將getActionCommand()與匹配的String值進行比較?

[英]Why can't I compare getActionCommand() with a matching String value?

我正在將計算器作為Java小程序進行。 我已經成功創建了布局,以及將actionListener注冊到所有JButton。

因此,在實現ActionListener的類中,所有數字按鈕均具有以下代碼:

String buttonClicked = e.getActionCommand();
// for number buttons
for(int i = 0; i <= 9; i++)
{
if(i == Integer.parseInt(buttonClicked) )
answer.setText(answer.getText() + e.getActionCommand() );
}

...可以正常工作。 所以,為什么這段代碼行不通:

if(buttonClicked.equals("/") )
{
answer.setText(answer.getText() + e.getActionCommand() );
}

...我們是否無法將字符串與getActionCommand()字符串內容進行比較? 還是我可能不導入我應該導入的內容?

編輯-這是我完整的代碼:

` 
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;

public class Part_C extends Applet
{
    // answer field here (no panel; JTextField is 100% width without panel)
    private JTextField answer = new JTextField();
    //answer.setHorizontalAlignment(JTextField.RIGHT); // align input to the right

    // JButton array
    private JButton[] buttons =
    {
        // (panel 1) backspace, clear entry, and clear buttons here
        new JButton("backspace"),
        new JButton("CE"),
        new JButton("C"),

        // (panel 2, row 1) numbers 7 - 9, divide, and sqare-root buttons here
        new JButton("7"),
        new JButton("8"),
        new JButton("9"),
        new JButton("/"),
        new JButton("sqrt"),

        // (panel 2, row 2) numbers 4 - 6, multiply, and percent buttons here
        new JButton("4"),
        new JButton("5"),
        new JButton("6"),
        new JButton("X"),
        new JButton("%"),

        // (panel 2, row 3) numbers 1 - 3, subtract, and inverse buttons here
        new JButton("1"),
        new JButton("2"),
        new JButton("3"),
        new JButton("-"),
        new JButton("1/x"),

        // (panel 2, row 4) number 0, positive/negative, decimal, addition, and equals buttons here
        new JButton("0"),
        new JButton("+/-"),
        new JButton("."),
        new JButton("+"),
        new JButton("="),

    }; // end of JButton array

    // constructor for class Part_C
    public Part_C()
    {
        // first panel for backspace, clear entry, and clear buttons
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1, 3) );
        for(int i = 0; i < 3; i++)
            panel1.add(buttons[i] );

        // second panel for operators and operands
        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(4, 5) );
        for(int i = 3; i < buttons.length; i++)
            panel2.add(buttons[i] );

        // third panel for BorderLayout of answer JTextField, and first 2 panels
        JPanel panel3 = new JPanel(new BorderLayout() );
        panel3.add(answer, BorderLayout.NORTH);
        panel3.add(panel1, BorderLayout.CENTER);
        panel3.add(panel2, BorderLayout.SOUTH);
        add(panel3); // add panel3 to the JFrame

        // action listener created here
        ButtonListener listener = new ButtonListener();

        // action listener is added to all JButtons here
        for (int i = 0; i < buttons.length; i++)
            buttons[i].addActionListener(listener);
    }

    /* overriding the init() method of Applet;
     * ...in an Applet, init() is used instead
     * of the "main" method */
    public void init()
    {
        // create JFrame
        Part_C frame = new Part_C();

        // the applet stops when the window / tab is closed
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // inner class ButtonListener
    public class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String buttonClicked = e.getActionCommand();

            // for number buttons
            for(int i = 0; i <= 9; i++)
            {
                if(i == Integer.parseInt(buttonClicked) )
                    answer.setText(answer.getText() + e.getActionCommand() );
            }

            // for operators
            if(buttonClicked.equals("/") )
            {
                answer.setText(answer.getText() + e.getActionCommand() );
            }
            else if(buttonClicked.equals("sqrt") )
            {
                answer.setText(answer.getText() + e.getActionCommand() );
            }
            else if(buttonClicked.equals("X") )
            {
                answer.setText(answer.getText() + e.getActionCommand() );
            }
            else if(buttonClicked.equals("%") )
            {
                answer.setText(e.getActionCommand() );
            }
            else if(buttonClicked.equals("-") )
            {
                answer.setText(answer.getText() + e.getActionCommand() );
            }
            else if(buttonClicked.equals("1/x") )
            {
                answer.setText(e.getActionCommand() );
            }
            else if(buttonClicked.equals("+/-") )
            {
                answer.setText(e.getActionCommand() );
            }
            else if(buttonClicked.equals(".") )
            {
                answer.setText(answer.getText() + e.getActionCommand() );
            }
            else if(buttonClicked.equals("+") )
            {
                answer.setText(e.getActionCommand() );
            }

            // for data-clearing operations
            if(buttonClicked.equals("backspace") )
            {
                answer.setText(answer.getText().substring(0, answer.getText().length() - 1) );
            }
            else if(buttonClicked.equals("CE") )
            {

            }
            else if(buttonClicked.equals("C") )
            {
                answer.setText("");
            }

            // for the equals button
            if(buttonClicked.equals("=") )
            {

            }
        }
    } // end of inner class buttonListener
} // end of Part_C

您正在嘗試對“ /”進行ParseInt。 這將引發NumberFormatException。 如果字符串不能轉換為整數,則您的代碼永遠不會超出for循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM