java类型javax。摆动JComponent无法解析
我正在使用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文件间接引用的”
第二个是,“类型容器中的add(String,Component)方法不适用于参数(JButton,String)”
谁能帮我解决这个问题吗?我对JAVA非常陌生,如果您能帮我一点忙,我将不胜感激。谢谢
# 1 楼答案
我也有同样的问题。我的代码运行良好,直到(我认为)我允许Java从7u25更新到8u51。这也可能是你的问题
尝试卸载较新版本的Java并重新安装较旧版本。比如7u25
我不确定swing组件是否已被去除(并被丢弃),但这将是我对为什么停止工作的猜测