3

I've library of JRE System Library JRE 1.8.x Here an error goes with my lines.

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

How can I remove this error? Here is screenshot: http://i60.tinypic.com/15z1n2h.png

enter image description here

Kindly tell me steps. I am new to Java. Thanks in advance.

. . Package Snake **Class RenderPanel

package snake;
import javax.swing.JPanel;
import java.awt.Graphics;
import javax.swing.JComponent;

@SuppressWarnings("serial")
public class RenderPanel extends JPanel{
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
    }
}

Class Snake

package snake;
import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JComponent;

public class Snake {

    public JFrame jframe; 

    public Toolkit toolkit;

    public static Snake snake;

    public Snake() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        toolkit = Toolkit.getDefaultToolkit();
        jframe = new JFrame("Snake");
        jframe.setVisible(true);
        jframe.setSize(800,700);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setLocation(dim.width / 2 - jframe.getWidth() / 2, dim.height / 2 - jframe.getHeight() / 2);
    }

    public static void main(String[] args)
    {
        snake = new Snake();
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • For instance, please look at [this similar question](http://stackoverflow.com/q/22002636/522444). – Hovercraft Full Of Eels Apr 27 '15 at 16:34
  • 1
    try to add `import javax.swing.JComponent;` otherwise your jdk is not on the classpath – Khinsu Apr 27 '15 at 16:34
  • Here are the errors Sir, Line 7: Multiple markers at this line - The type javax.swing.JComponent cannot be resolved. It is indirectly referenced from required .class files - The hierarchy of the type RenderPanel is inconsistent Line 12: The method paintComponent(Graphics) is undefined for the type JPanel – David Stevenson Apr 27 '15 at 16:56
  • When I added import java.swing.JComponent; ------------ it gave error: "The import javax.swing.JComponent cannot be resolved" – David Stevenson Apr 27 '15 at 16:59
  • same error with import javax.swing.JComponent; – David Stevenson Apr 27 '15 at 17:07
  • 1
    Please check out the [help] as well as the [How Do I Ask Good Questions](http://stackoverflow.com/help/how-to-ask) sections to see why this is important and what you can do to improve your question for us. – Hovercraft Full Of Eels Apr 27 '15 at 17:09
  • @DavidStevenson resuming all recommendations: **1)** Add your code to your question. **2)** Add the full error message to your question. **3)** Look at [this similar question](http://stackoverflow.com/q/22002636/522444). – Frakcool Apr 27 '15 at 17:10
  • Thanks Sir. I edited my this "first" question. – David Stevenson Apr 27 '15 at 17:13
  • 1
    **Many** thanks for the updated information. Check your Eclipse Build path. Please also look at [this similar question](http://stackoverflow.com/questions/3464592/getting-the-following-build-error-the-type-cannot-be-resolved-it-is-ind). – Hovercraft Full Of Eels Apr 27 '15 at 17:17
  • 1
    Sorry if we "sound" rude, it's just we're voluntiers and don't get paid for this. So time is important. All we want is that you help us to help you. – Frakcool Apr 27 '15 at 17:17
  • Ya thanks.. my path is correct. if you are asking the path from "My Computer/Settings/Variable Environment" then it is correct. – David Stevenson Apr 27 '15 at 17:20
  • David, no, I think that your problem may be related to how Eclipse has been set up, which is why I've added an Eclipse tag to your question. The build path is a path that eclipse uses to find the necessary files needed to run your programs. – Hovercraft Full Of Eels Apr 27 '15 at 17:21

1 Answers1

1

You need to "import" classes in each file in order to use them.

You have imported JPanel but not JComponent.

Under your import JPanel line add another one that looks the same but has JComponent at the end instead of JPanel.

Tim B
  • 40,716
  • 16
  • 83
  • 128