java - 我的 Append 方法在 Java 中不起作用

标签 java

我有一个程序正在尝试制作计算器,但附加方法不起作用。编译器会给我这个错误:无法找到符号 - 方法append(java.lang.String)

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Calculator extends Frame implements ActionListener,WindowListener
{
    Button one,two,three,four,five,six,seven,eight,nine,zero,plus,minus,divide,times,equals,one2,two2,three2,four2,five2,six2,seven2,eight2,nine2,zero2;

TextField numOne,operation,numTwo;

Label fill;
public static void main(String[] args)
{
    Calculator calc = new Calculator("Calculator");
    calc.setVisible(true);
    calc.setSize(380,153);
    calc.setLocationRelativeTo(null);
    calc.setBackground(Color.white);
}

public Calculator(String title)
{
    super(title);
    setLayout(new FlowLayout(FlowLayout.LEFT));
    addWindowListener(this);

    one = new Button("1");
    two = new Button("2");
    three = new Button("3");
    four = new Button("4");
    five = new Button("5");
    six = new Button("6");
    seven = new Button("7");
    eight = new Button("8");
    nine = new Button("9");
    zero = new Button("0");

    one2 = new Button("1");
    two2 = new Button("2");
    three2 = new Button("3");
    four2 = new Button("4");
    five2 = new Button("5");
    six2 = new Button("6");
    seven2 = new Button("7");
    eight2 = new Button("8");
    nine2 = new Button("9");
    zero2 = new Button("0");

    minus = new Button("-");
    plus = new Button("+");
    divide = new Button("/");
    times = new Button("X");

    equals = new Button("=");

    numOne = new TextField(10);
    operation = new TextField(10);
    numTwo = new TextField(10);

    fill = new Label("                                ");

    add(numOne);
    add(one);
    add(two);
    add(three);
    add(four);
    add(five);
    add(six);
    add(seven);
    add(eight);
    add(nine);
    add(zero);

    add(operation);
    add(minus);
    add(plus);
    add(divide);
    add(times);

    add(fill);
    add(numTwo);
    add(one2);
    add(two2);
    add(three2);
    add(four2);
    add(five2);
    add(six2);
    add(seven2);
    add(eight2);
    add(nine2);
    add(zero2);

    add(equals);

    one.addActionListener(this);
    two.addActionListener(this);
    three.addActionListener(this);

    minus.addActionListener(this);

    four.addActionListener(this);
    five.addActionListener(this);
    six.addActionListener(this);

    plus.addActionListener(this);

    seven.addActionListener(this);
    eight.addActionListener(this);
    nine.addActionListener(this);

    one2.addActionListener(this);
    two2.addActionListener(this);
    three2.addActionListener(this);
    four2.addActionListener(this);
    five2.addActionListener(this);
    six2.addActionListener(this);
    seven2.addActionListener(this);
    eight2.addActionListener(this);
    nine2.addActionListener(this);
    zero2.addActionListener(this);

    times.addActionListener(this);

    equals.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
    //This is where my append method won't work
    if(e.getSource() == one)
    {
        numOne.append("1");
    }
    if(e.getSource() == two)
    {
        numOne.append("2");
    }
    if(e.getSource() == three)
    {
        numOne.append("3");
    }
    if(e.getSource() == four)
    {
        numOne.append("4");
    }
    if(e.getSource() == five)
    {
        numOne.append("5");
    }
    if(e.getSource() == six)
    {
        numOne.append("6");
    }
    if(e.getSource() == seven)
    {
        numOne.append("7");
    }
    if(e.getSource() == eight)
    {
        numOne.append("8");
    }
    if(e.getSource() == nine)
    {
        numOne.append("9");
    }
    if(e.getSource() == zero)
    {
        numOne.append("0");
    }
    if(e.getSource() == minus)
    {
        operation.setText("-");
    }
    if(e.getSource() == times)
    {
        operation.setText("X");
    }
    if(e.getSource() == plus)
    {
        operation.setText("+");
    }
    if(e.getSource() == divide)
    {
        operation.setText("/");
    }
    if(e.getSource() == one2)
    {
        numTwo.append("1");
    }
    if(e.getSource() == two2)
    {
        numTwo.append("2");
    }
    if(e.getSource() == three2)
    {
        numTwo.append("3");
    }
    if(e.getSource() == four2)
    {
        numTwo.append("4");
    }
    if(e.getSource() == five2)
    {
        numTwo.append("5");
    }
    if(e.getSource() == six2)
    {
        numTwo.append("6");
    }
    if(e.getSource() == seven2)
    {
        numTwo.append("7");
    }
    if(e.getSource() == eight2)
    {
        numTwo.append("8");
    }
    if(e.getSource() == nine2)
    {
        numTwo.append("9");
    }
    if(e.getSource() == zero2)
    {
        numTwo.append("0");
    }
}

public void windowClosing(WindowEvent e)
{
    dispose();
    System.exit(0);
}

public void windowOpened(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

}

最佳答案

您正在对 TextField 对象调用 append() 方法,而该对象没有该方法。尝试使用 setText("string") 代替。

关于java - 我的 Append 方法在 Java 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344394/

相关文章:

java - Lombok @Wither/@With Inheritance(超/子类)

java - Stellar 支付代码在 Java 中不起作用

java - 需要有关 C++ 到 Java 翻译的帮助

Java,代号一 : Exporting and -most importantly- importing files between users

java - 将 BufferedImage 转换/写入为 postscript

java - Android HttpPost文件上传500内部服务器错误

java - wait() 或 notifyAll() 创建无限循环

java - 将数据库中的多个值添加到 Rich List Blackberry

java - 单击一个菜单即可更改所有 Activity 的语言

java - 如何利用 hibernate c3p0连接池?