java - 尝试为一个简单的计算器编写代码。当用户按下按钮时,无法在文本框中显示文本

标签 java gettext settext

在代码末尾,我使用 getActionCommand 编写 if 命令来查看用户按下的按钮。我想让用户按下的任何内容都将添加到文本框中,以便他们可以看到他们正在输入的内容。出于某种原因,无论我首先按下的数字似乎都会显示此后所有其余的数字。我该如何解决这个问题?

//Michael Moradi
//Period C1
//June 2, 2015
//Final Project Semester 2

import javax.swing.*; //imports all that is needed for the code

import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener {

    public JButton button1, button2, button3, button4, button5, button6, button7, 
                   button8, button9, button10, button11, button12, button13, button14, 
                   button15, button16;
    public JTextArea text;

    public Calculator()
    {
        setSize(350,300); //sets size to 300 by 300 //LOOK UP HOW TO LOCK SIZE
        setResizable(false); //does not let user change the size of the window
        setDefaultCloseOperation(EXIT_ON_CLOSE);//makes app close when I press the x on the top left

        Container contentPane = getContentPane();//gets the contentPane
        contentPane.setBackground(Color.CYAN);//sets background color to white         

        contentPane.setLayout(new FlowLayout());//makes the contentPane read from left to right

        text = new JTextArea(1, 25);
        contentPane.add(text);
        text.setEditable(false);

        button1 = new JButton ("7");
        contentPane.add(button1);
        button1.addActionListener(this);

        button2 = new JButton ("8");
        contentPane.add(button2);
        button2.addActionListener(this);

        button3 = new JButton ("9");
        contentPane.add(button3);
        button3.addActionListener(this);

        button4 = new JButton ("÷");
        contentPane.add(button4);
        button4.addActionListener(this);

        button5 = new JButton ("4");
        contentPane.add(button5);
        button5.addActionListener(this);

        button6 = new JButton ("5");
        contentPane.add(button6);
        button6.addActionListener(this);

        button7 = new JButton ("6");
        contentPane.add(button7);
        button7.addActionListener(this);

        button8 = new JButton ("x");
        contentPane.add(button8);
        button8.addActionListener(this);

        button9 = new JButton ("1");
        contentPane.add(button9);
        button9.addActionListener(this);

        button10 = new JButton ("2");
        contentPane.add(button10);
        button10.addActionListener(this);

        button11 = new JButton ("3");
        contentPane.add(button11);
        button11.addActionListener(this);

        button12 = new JButton ("-");
        contentPane.add(button12);
        button12.addActionListener(this);

        button13 = new JButton ("0");
        contentPane.add(button13);
        button13.addActionListener(this);

        button14 = new JButton (".");
        contentPane.add(button14);
        button14.addActionListener(this);

        button15 = new JButton ("=");
        contentPane.add(button15);
        button15.addActionListener(this);

        button16 = new JButton ("+");
        contentPane.add(button16);
        button16.addActionListener(this);



    }

    public static void main(String[] args)
    {
        Calculator guiWindow = new Calculator(); //uses GUI
        guiWindow.setVisible(true); //makes it visible

    }

    public void actionPerformed(ActionEvent e)
    {
        Container contentPane = getContentPane();

        String enteredNumbers = text.getText();

        if (e.getActionCommand().equals("7"));
        text.setText(enteredNumbers + ("7"));

        if (e.getActionCommand().equals("8"));
        text.setText(enteredNumbers + ("8"));

        if (e.getActionCommand().equals("9"));
        text.setText(enteredNumbers + ("9"));

        if (e.getActionCommand().equals("÷"));
        text.setText(enteredNumbers + ("÷"));

        if (e.getActionCommand().equals("4"));
        text.setText(enteredNumbers + ("4"));

        if (e.getActionCommand().equals("5"));
        text.setText(enteredNumbers + ("5"));

        if (e.getActionCommand().equals("6"));
        text.setText(enteredNumbers + ("6"));

        if (e.getActionCommand().equals("x"));
        text.setText(enteredNumbers + ("x"));

        if (e.getActionCommand().equals("1"));
        text.setText(enteredNumbers + ("1"));

        if (e.getActionCommand().equals("2"));
        text.setText(enteredNumbers + ("2"));

        if (e.getActionCommand().equals("3"));
        text.setText(enteredNumbers + ("3"));

        if (e.getActionCommand().equals("-"));
        text.setText(enteredNumbers + ("-"));

        if (e.getActionCommand().equals("0"));
        text.setText(enteredNumbers + ("0"));

        if (e.getActionCommand().equals("."));
        text.setText(enteredNumbers + ("."));

        if (e.getActionCommand().equals("="));
        text.setText(enteredNumbers + ("="));

        if (e.getActionCommand().equals("+"));
        text.setText(enteredNumbers + ("+"));



    }

}

最佳答案

每个 if 语句后面都有分号。这个分号是一个空语句,因为if语句末尾不需要分号。空语句(只是一个分号)不执行任何操作并正常完成,不会出现错误。

现在,if 语句末尾的空语句意味着,如果 if 语句的表达式计算结果为 true,则无论 if 语句是否返回 true,都不会执行任何操作,并执行下一条语句。

所以而不是

if (e.getActionCommand().equals("7"));
    text.setText(enteredNumbers + ("7"));

用这个

if (e.getActionCommand().equals("7"))
    text.setText(enteredNumbers + ("7"));

关于java - 尝试为一个简单的计算器编写代码。当用户按下按钮时,无法在文本框中显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30549358/

相关文章:

java - Spring Boot 管理服务器 : App Specific Email Notification

c# - 我可以限制 C# 函数仅接受字符串文字吗?

android - 设置微调器所选项目文本

java - 字典序最小排列,使得所有相邻字母都不同

java - 我们什么时候应该在方法中抛出异常或捕获异常?

c - fputs( _ ("") ) 下划线代表什么?

java - 需要一些帮助才能通过 setText 问题将变量从 fragment X 传递到 fragment Y

java - 如何清除 JTextArea?

java - 控制射速 (Java GameDev)

php - 有没有解析gettext PO POT文件的PHP库?