gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:12:48 25 4
gpt4 key购买 nike

我有一个试图制作计算器的程序,但附加方法不起作用。编译器会给我这个错误:找不到符号 - 方法追加(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com