Java swing component cannot be resolved

I extracted the following code from the 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); Two errors are highlighted and displayed:

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

  • The javax.swing.JComponent type cannot be resolved. This indirectly refers to the required .class files

Being relatively new to java, I don't quite understand what Eclipse is trying to tell me. What can I do to make it work?

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

+6
source share
7 answers

Obviously, the problem was not in the code because it worked for everyone else. So I decided to uninstall and reinstall java and eclipse, et voilΓ ! Now it works. Thank you all for your feedback.

+5
source

Just guessing, but it matches the problem you described:

This problem may occur if your own package contains a class called JLabel . The local class then obscures the imported JLabel class from the javax.swing package and may not have the same class hierarchy as the original class.

To summarize: it is usually a bad idea to name your own classes, such as existing JDK classes.

+3
source

I ran into the same problem and found that my project was using Java 8 libraries. I installed the Java 7 library, added the same to the project properties, specifying the library from the installed folder C:\Program Files\Java , and the error went away.

Looks like the new version of Java 8 might allow the swing component differently.

+3
source

I am getting this problem too. Unfortunately, to solve it, I needed to establish my eclipse. but I think the problem was incompatibility between the JRE and the JAVA version in Eclipse .

I used JRE supported by JAVA8 when my project (and my eclipse indigo) cannot switch to JAVA8. An update for another eclipse that java 8 can support solves the problem.

+2
source
  • Eclipse must have the JDK installed (and selected):

    Window> Preferences> Java> Installed JREs

  • The project must be configured to use this JDK:

    Project> Properties> Java Build Path> Libraries

  • the list should contain an entry similar to "JRE System Library [JDK7]". If you do not click:

    Add Library ...> JRE System Library

  • The project was to be created as a "Java Project":

    Project> Properties> Builders

    should include "Java Builder"

SoCodeIt IDE Dr

+1
source

I'm new here. I had this problem and it took me 2 days of research. I solved this by adding an external JAR file containing all the necessary components for my Eclipse. Right-click the project -> Buildpath -> Configure Build Path -> Add External JARS and view the JAR file on your computer. JAR file can be downloaded here http://www.java2s.com/Code/Jar/j/Downloadjsdgstubsjre15jar.htm Hope this helps someone out there.

+1
source

I also ran into the same problem in the eclipse environment, but after changing the jar file to jre7 (right-click on the project β†’ Build path β†’ Configure build path β†’ AddLibrary β†’ Jre System Library), the error resolved, I don’t know how this happens can someone explain this.

0
source

Source: https://habr.com/ru/post/982672/


All Articles