简体   繁体   中英

The import java.awt.BorderLayout cannot be resolved

I created a new Java project on Eclipse and got the The import java.awt.BorderLayout cannot be resolved error. What went wrong and how do I fix this?

I created a new JFrame from WindowsBuilder and the default code generated was:

import java.awt.BorderLayout; //error here
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class frameGUI extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frameGUI frame = new frameGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public frameGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0)); //error here
        setContentPane(contentPane);
    }

}

I'm also getting a BorderLayout cannot be resolved to a type error.

@Mark Rotteveel answered: It looks like your code is in the default, unnamed package. IIRC (but I'm not 100% sure), you can't use the module system in combination with the unnamed package. Move your code to a package.

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