无法将JButton添加到JFrame

3
我正在按照此YouTube教程(https://www.youtube.com/watch?v=DFQzFJqOSbA&index=83&list=PLFE2CE09D83EE3E28)进行操作,但是我在这个错误下看到了带有红色下划线的add(lb):

类型容器中的方法add(Component)不适用于参数(JButton)。

我感到困惑,因为根据这个链接,JButton扩展自Container,那么它不应该是有效的参数吗?

我感觉有些愚蠢的错误被忽略了,请帮忙解答,谢谢。

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Layout extends JFrame {
    private JButton lb;
    private JButton cb;
    private JButton rb;
    private FlowLayout layout;
    private Container container;

    public Layout() {
        super("title");
        layout = new FlowLayout();
        container = getContentPane();
        setLayout(layout);

        lb = new JButton("left");
        add(lb);
    }
}

当我将鼠标悬停在 lb = new JButton("left") 上时,弹出窗口显示的内容如下...
Open Declaration javax.swing.JButton.JButton(String arg0)
@ConstructorProperties(value={"text"})

注意:该元素既没有附加源码,也没有附加Javadoc,因此无法找到任何Javadoc。
编辑:
我的导入行也有一个错误,它说:
The type javax.swing.JComponent cannot be resolved. 
It is indirectly referenced from required .class files

这是我项目的屏幕截图: puu.sh/jBIt3/02a27cb22b.png puu.sh/jBIuY/5a56f7426e.png puu.sh/jBIvY/7b94dc5782.png

3
1)请提供完整的错误信息,包括有问题的类的完整包名。 2)您在同一个包中是否有另一个名为“JButton”的类? - Hovercraft Full Of Eels
2
你的代码在我的IDE上编译通过了。也许你应该清理/重建你的项目。 - RealSkeptic
3
您应该查看此问题,他们提供的答案指出了与他们的jdk有关的问题。 - Alex S. Diaz
3
这与您的程序是否正确无关,而是Eclipse中配置的JRE出了问题。请前往“窗口/首选项”,然后选择“Java/已安装的JRE”,删除您正在使用的JRE,再重新添加即可。 - Roberto Attias
2
好的,我刚刚升级到 Mars 版本,我的问题已经解决了。我不知道我怎么会用上 Helios 版本,因为我没有安装 Eclipse 很久了,可能是我点错了。谢谢大家的帮助。 - pyjamas
显示剩余5条评论
1个回答

0

重新安装了更新的 Eclipse 版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接