我正在使用 ACM 库并自己完成“Java 的艺术与科学”中的练习。我有一个简单的程序,应该在屏幕上添加一个按钮,但我收到了一些我自己无法解决的错误消息。
//The first import results in the error message about javax.swing.JComponent
import acm.program.*;
import java.awt.event.*;
import javax.swing.*;
public class NewFirstButton extends ConsoleProgram {
public void init(){
setFont("Courier-24");
hiButton = new JButton("Hi");
// The line below produces the second error about function args
add(hiButton, SOUTH);
addActionListeners();
}
public void actionPerformed(ActionEvent e){
if (hiButton == e.getSource()){
println("Hello there!");
}
}
private JButton hiButton;
}
第一个问题是错误消息“无法解析类型 javax.swing.JComponent。它是从所需的 .class 文件中间接引用的”。
第二个是“Container 类型中的方法 add(String, Component) 不适用于参数 (JButton, String)”。
谁能帮我解决这些问题?我对 JAVA 非常陌生,非常感谢您的帮助。谢谢