java swing组件无法解析 [英] java swing component cannot be resolved

查看:796
本文介绍了java swing组件无法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从教程中获取了以下代码:

I took the following code from a tutorial:

import javax.swing.*;
import java.util.Date;

public class SwingGUI {

    public static void main( String[] args )
      {
        JFrame f = new JFrame( "test" );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.setSize( 1500, 900 );
        JLabel l = new JLabel( String.format( "%tT", new Date() ) );
        f.add(l);
        f.setVisible( true );
      }
}

f.add( l); 突出显示并显示两个错误:

The f.add(l); is highlighted and two errors are shown:


  • 方法add(Component)in类型Container不适用于参数(JLabel)

  • The method add(Component) in the type Container is not applicable for the arguments (JLabel)

无法解析javax.swing.JComponent类型。它是从所需的.class文件间接引用的。

The type javax.swing.JComponent cannot be resolved. It is indirectly referenced from required .class files

对java来说相对较新,我真的不明白Eclipse是什么想告诉我。我该怎么做才能使它工作?

Being relatively new to java, I do not really understand what Eclipse is trying to tell me. What can I do to make it work?

编辑:代码运行时没有行 f.add(l); ,所以问题不在于找不到JFrame或JLabel。在摆弄了一点之后,我摆脱了第一个错误,但第二个仍然存在。组件无法解析,因为它是间接引用的。这是什么意思?

edit: The code runs without the line f.add(l);, so the problem is not that JFrame or JLabel are not found. After fiddeling around a little I got rid of the first error, but the second still remains. The component cannot be resolved, because it is indirectly referenced. What does that mean?

推荐答案

显然问题不在于代码,因为它适用于其他所有人。所以我决定删除并重新安装java和eclipse,etvoilà!现在它有效。谢谢大家的反馈。

Obviously the problem did not lie in the code, because it worked for everyone else. So I decided do remove and re-install java and eclipse, et voilà! Now it works. Thank you all for your feedback.

这篇关于java swing组件无法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆