JPanel error - J Component cannot be resolved

ぐ巨炮叔叔 提交于 2019-12-13 15:13:36

问题


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

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

回答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.



来源:https://stackoverflow.com/questions/29901132/jpanel-error-j-component-cannot-be-resolved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!