我的追加方法不适用于Java

时间:2014-01-24 23:31:31

标签: java

我有一个程序,我正在尝试制作一个计算器,但是追加方法不起作用。编译器会给我这个错误:找不到符号 - 方法追加(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) {}

}

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

numOne是一个TextField,没有这样的方法。请参阅http://docs.oracle.com/javase/7/docs/api/java/awt/TextField.html

答案 2 :(得分:0)

您正在调用的方法append()TextArea控件的方法。您需要setText()控件的TextField方法。

<强>来源

http://docs.oracle.com/javase/tutorial/uiswing/components/textarea.html http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

答案 3 :(得分:0)

你可以在字符串之间串联,如下:

numOne.setText(numOne.getText() + "1" );