简体   繁体   中英

Eclipse: add JPanel in JFrame

I wrote a simple project about Swing (without action listener). I want to add a button to a JPanel and add the panel (named panel ) to a JFrame (named frame ). But when I added the panel to the frame, Eclipse had an error:

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

This is my source:

import javax.swing.*;

public class Click {


    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("oooooo");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();

    frame.add(panel); //the error
    }
}

What should I do?

It seems there is an issue with JRE your eclipse is running on and jre build path is pointing. I suggest you to follow steps:

  1. Right click on your project -> Build Path -> Configure Build Path
  2. Go to 'Libraries' tab
  3. Remove Previous Version
  4. Add Library -> JRE System Library -> Next -> Workspace default JRE (or you can Choose Alternate JRE from your System) -> Finish

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM