1

我正在使用 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 非常陌生,非常感谢您的帮助。谢谢

4

1 回答 1

1

我有同样的问题。我的代码运行良好,直到(我认为)我允许 Java 从 7u25 更新到 8u51。那也可能是你的问题。

尝试卸载较新版本的 Java 并重新安装较旧版本。比如7u25。

我不确定摆动组件是否已被贬低(并丢弃),但这将是我对为什么事情停止工作的猜测。

于 2015-10-18T04:49:11.947 回答