java swing组件无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28582102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:28:07  来源:igfitidea点击:

java swing component cannot be resolved

javaswing

提问by Ludwitch

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 );
      }
}

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

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

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

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

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

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

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?

作为 Java 的新手,我不太明白 Eclipse 试图告诉我什么。我该怎么做才能让它发挥作用?

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?

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

采纳答案by Ludwitch

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 和 eclipse,等等!现在它起作用了。感谢大家的反馈。

回答by piet.t

Just guessing, but it matches the problem you describe:

只是猜测,但它符合您描述的问题:

This problem can arise when your own package contains a class that is called JLabel. The local class will then shadow the imported JLabel-class from the javax.swing-package and it might not have the same class-hierarchy as the original class.

当您自己的包包含名为 的类时,可能会出现此问题JLabel。然后,本地类将从JLabel-package 中javax.swing隐藏导入的-class,并且它可能与原始类具有不同的类层次结构。

To sum it up: it usaually is a bad idea to name ones own classes like existing JDK-classes.

总结一下:将自己的类命名为现有的 JDK 类通常是一个坏主意。

回答by Anand Nanavati

I faced the same issue and found that my project was using the Java 8 libraries. I installed the Java 7 library, added the same to the project properties by referencing the library from the installed C:\Program Files\Javafolder and the error was gone.

我遇到了同样的问题,发现我的项目使用的是 Java 8 库。我安装了 Java 7 库,通过引用已安装C:\Program Files\Java文件夹中的库将相同的内容添加到项目属性中,错误消失了。

Looks like, the new Java 8 version may a different way to resolve the swing component.

看起来,新的Java 8 版本可能会以不同的方式来解决swing 组件。

回答by Praveen

  1. There must be an installed (and selected) JDK in Eclipse:

    Window > Preferences > Java > Installed JREs

  2. The project must be configured to use that JDK:

    Project > Properties > Java Build Path > Libraries

  3. there should be an entry like "JRE System Library [JDK7]" in the list. If not press:

    Add Library... > JRE System Library

  4. The project should have been created as a "Java Project":

    Project > Properties > Builders

    should include "Java Builder"

  1. Eclipse 中必须安装(并选择)一个 JDK:

    窗口 > 首选项 > Java > 已安装的 JRE

  2. 该项目必须配置为使用该 JDK:

    项目 > 属性 > Java 构建路径 > 库

  3. 列表中应该有一个类似于“JRE 系统库 [JDK7]”的条目。如果没有按:

    添加库... > JRE 系统库

  4. 该项目应该已创建为“Java 项目”:

    项目 > 属性 > 构建器

    应该包括“Java Builder”

SoCodeItIDE Dr

SoCodeIt IDE 博士

回答by cohen

I get this problem too. unfortunately to resolve it I was needed to install my eclipse. but I think that the problem was incompatibility between JRE and JAVA version in Eclipse.

我也遇到这个问题。不幸的是,为了解决它,我需要安装我的 eclipse。但我认为问题是Eclipse 中 JRE 和 JAVA 版本之间的不兼容

I used JRE supported JAVA8 when my project (and my eclipse indigo) can't upgrade to JAVA8. Updating to another eclipse that can support java 8 solve the problem.

当我的项目(和我的 eclipse indigo)无法升级到 JAVA8 时,我使用了 JRE 支持的 JAVA8。更新到另一个可以支持 java 8 的 eclipse 解决了这个问题。

回答by lookAheadAssertion

am new here. I had this problem and it took me 2 days of research. I solved it by adding an external JAR file that contains all the required components to my Eclipse. Right-click project>>Buildpath>>Configure build path>> Add external JARS and browse for the JAR file on your computer. The JAR file can be downloaded here http://www.java2s.com/Code/Jar/j/Downloadjsdgstubsjre15jar.htmHope this helps someone out there.

我是新来的。我遇到了这个问题,我花了 2 天的时间研究。我通过向 Eclipse 添加一个包含所有必需组件的外部 JAR 文件来解决它。右键单击项目>>构建路径>>配置构建路径>>添加外部 JARS 并在您的计算机上浏览 JAR 文件。JAR 文件可以在这里下载http://www.java2s.com/Code/Jar/j/Downloadjsdgstubsjre15jar.htm希望这对那里的人有所帮助。

回答by Taleev Aalam

I was also facing the same problem in eclipse IDE but after changing the jar file to jre7(Right-click project>>Buildpath>>Configure build path>> AddLibrary>>Jre System Library) the error has resolved, i don't know how it happens can some one explain it.

我在 eclipse IDE 中也遇​​到了同样的问题,但是在将 jar 文件更改为 jre7(右键单击项目>>构建路径>>配置构建路径>>添加库>>Jre 系统库)后,错误已解决,我不知道有人可以解释它是如何发生的。

回答by Prakash Kaushal

The problem is not in code.

问题不在代码

It is due to JRE misconfiguration for project. It may be due to difference between the project's JRE and workspace's Jre.

这是由于项目的 JRE 配置错误。这可能是由于项目的 JRE 和工作区的 Jre 之间存在差异。

It may be solved by Updating Eclipse or Jre...

可以通过更新 Eclipse 或 Jre来解决...

But from my side, I recommend you to do so..

但从我的角度来看,我建议你这样做..

Goto Window-->Preferences-->Java-->Installed JREs--->Execution Environments-->Select Project's JRE and tick the perfect matching installed JRE.

转到Window-->Preferences-->Java-->Installed JREs--->Execution Environments-->Select Project's JRE 并勾选完美匹配的 installed JRE

If you have not a matching JRE download and add it in Installed JREs using Add Button.

如果您没有匹配的 JRE 下载并使用添加按钮将其添加到已安装的 JRE 中。

At last, RESTART your Eclipse IDE.This worked fine for me...

最后,重新启动 Eclipse IDE。这对我来说很好...

If it does not work, UPDATE Eclipse..

如果它不起作用,请更新 Eclipse..

回答by B.K

Updating the execution environment under system library to JavaSE-1.7 or JavaSE-1.8 in eclipse should solve the problem. To update the step below can be followed:

在eclipse中将system library下的执行环境更新为JavaSE-1.7或JavaSE-1.8应该可以解决问题。要更新可以遵循以下步骤:

> Right-click on the project
> Select properties
> Java build path
> Library > Add Library > Add JRE SYSTEM Library
> Execution Environment
> Select JavaSE-1.7 or JavaSE-1.8
> Finish