'how to solve errors arising from the container when creating a GUI using Java [closed]

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUIWindow {
    public static void main(String[] args) {
        JFrame` theGUI = new JFrame();
           theGUI.setTitle("Visual studio code");
            theGUI .setSize(300,200);
            theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           JPanel northpanel  =new JPanel();
             northpanel .setBackground(Color.red);
           JPanel eastPanel = new JPanel();
        eastPanel .setBackground(Color.blue);
        JPanel westPanel = new JPanel();
        westPanel .setBackground(Color.blue);
        JPanel southPanel = new JPanel();
        southPanel .setBackground(Color.red);
        JPanel centerPanel = new JPanel();
        centerPanel .setBackground(Color.green);
        container pane =GUI.getContentPane();
        pane.add(northpanel, BorderLayout.NORTH);
        pane.add(eastPanel,BorderLayout.EAST);
        pane.add(westPanel,BorderLayout.WEST);
        pane.add(southPanel,BorderLayout.SOUTH);
        pane.add(centerPanel,BorderLayout.CENTER);
        theGUI.setVisible(true);

    } 
    
}

[{
    "resource": "/home/linc/Javaprojects/GUIWindow.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "570425394",
    "severity": 8,
    "message": "GUI cannot be resolved",
    "source": "Java",
    "startLineNumber": 23,
    "startColumn": 25,
    "endLineNumber": 23,
    "endColumn": 28
},{
    "resource": "/home/linc/Javaprojects/GUIWindow.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "67108964",
    "severity": 8,
    "message": "The method add(JPanel, String) is undefined for the type container",
    "source": "Java",
    "startLineNumber": 24,
    "startColumn": 14,
    "endLineNumber": 24,
    "endColumn": 17
},{
    "resource": "/home/linc/Javaprojects/GUIWindow.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "67108964",
    "severity": 8,
    "message": "The method add(JPanel, String) is undefined for the type container",
    "source": "Java",
    "startLineNumber": 25,
    "startColumn": 14,
    "endLineNumber": 25,
    "endColumn": 17
},{
    "resource": "/home/linc/Javaprojects/GUIWindow.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "67108964",
    "severity": 8,
    "message": "The method add(JPanel, String) is undefined for the type container",
    "source": "Java",
    "startLineNumber": 26,
    "startColumn": 14,
    "endLineNumber": 26,
    "endColumn": 17
},{
    "resource": "/home/linc/Javaprojects/GUIWindow.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "67108964",
    "severity": 8,
    "message": "The method add(JPanel, String) is undefined for the type container",
    "source": "Java",
    "startLineNumber": 27,
    "startColumn": 14,
    "endLineNumber": 27,
    "endColumn": 17
},{
    "resource": "/home/linc/Javaprojects/GUIWindow.java",
    "owner": "_generated_diagnostic_collection_name_#3",
    "code": "67108964",
    "severity": 8,
    "message": "The method add(JPanel, String) is undefined for the type container",
    "source": "Java",
    "startLineNumber": 28,
    "startColumn": 14,
    "endLineNumber": 28,
    "endColumn": 17

}]


Solution 1:[1]

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

I modified your code to create the following GUI.

enter image description here

All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that the Swing components are created and executed on the Event Dispatch Thread.

I separated the creation of the JFrame and the JPanels. You size each of the JPanels, ideally by filling each JPanel with Swing components. You pack the JFrame, letting it take the size of the JPanels. Pay close attention to the Laying Out Components Within a Container section.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUIWindow implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GUIWindow());
    }

    @Override
    public void run() {
        JFrame theGUI = new JFrame();
        theGUI.setTitle("Visual studio code");
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        theGUI.add(createPanel(Color.red), BorderLayout.NORTH);
        theGUI.add(createPanel(Color.blue), BorderLayout.EAST);
        theGUI.add(createPanel(Color.blue), BorderLayout.WEST);
        theGUI.add(createPanel(Color.red), BorderLayout.SOUTH);
        theGUI.add(createPanel(Color.green), BorderLayout.CENTER);
        
        theGUI.pack();
        theGUI.setLocationByPlatform(true);
        theGUI.setVisible(true);
    }
    
    private JPanel createPanel(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        panel.setPreferredSize(new Dimension(100, 100));
        
        return panel;
    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gilbert Le Blanc