java - 使用 getActionCommand 的 setText 问题

标签 java gettext settext

我是一名初学者程序员,正在尝试制作一个简单的计算器。在代码末尾,我尝试让文本字段显示用户按下的任何按钮。由于某种原因,text.setText("7") 没有显示在文本字段中。你能帮我解决这个问题吗?

    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

            JTextArea 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();

            if (e.getActionCommand().equals("7"));
            {
                text.setText("7");
            }






        }

    }

最佳答案

看起来您在构造函数内隐藏了成员字段JTextArea text。因此,这意味着您没有将 JTextArea 实例分配给成员字段,而是分配给构造函数中的局部变量。

用这个代替

JTextArea text = new JTextArea(1, 25);

这个

text = new JTextArea(1, 25);

关于java - 使用 getActionCommand 的 setText 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30543501/

相关文章:

java - 执行按钮单击操作时更新标签文本 (Java)

Android SetText 更改键盘类型

java - 我必须将输入文件放在 eclipse(java) 中的哪里才能写入它

java - org.eclipse.ui.internal.WorkbenchPage.showView 的 NullPointerException

java - AsyncTask 类中的标志在主类中无法正常工作

Java String 仅在字符串末尾保留数字字符

c++ - 用 C++ 编写国际化和本地化应用程序的基本资源是什么?

java - 如何使用字段中的 linkText 提取 WebElements 并单击它

php - 使用正则表达式从 po 文件获取翻译计数和模糊计数...

Android:setText 破坏格式化字符串