Question

Any idea why my code is not connected to the frame?

Frame:

import javax.swing.*;
import java.awt.*;

public class CourseGUI extends JFrame {
    public CourseGUI()
    {
        super("CourseGUI Frame");

        JPanel topPane = new TopPanel();
        JPanel topPanel = new JPanel();
        topPanel.setBackground(java.awt.Color.WHITE);
        Dimension d = new Dimension(800,600);

        topPanel.setPreferredSize(d);

        this.setLayout(new BorderLayout());

        this.add(topPanel, BorderLayout.NORTH);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setSize(800,600);

        this.setVisible(true);
    }
    public static void main(String[] args)
    {
        new CourseGUI();
    }

}

Panel:

import javax.swing.*;
import java.awt.*;


public class TopPanel extends JPanel {

    public TopPanel() {     
        TopPanel tp = new TopPanel();
        tp.add(new JLabel("Course Info"));
        tp.setSize(300,300);

        tp.setVisible(true);
    }

}

With the panel, I tried to get it where it will be on the NORTH of the Frame, sadly, it's not working. I am a beginner programmer, well In SCHOOL this year to actually learn this, Our teacher taught us this 4 days ago and I can not be anymore confused. Tried to get help several times on this, even from professor, with no avail, someone please help me out and explain. Thanks in advance.

Was it helpful?

Solution

public class TopPanel extends JPanel {

    public TopPanel() {     
        TopPanel tp = new TopPanel();
        tp.add(new JLabel("Course Info"));
        tp.setSize(300,300);

        tp.setVisible(true);
    }

}

By creating a TopPanel object inside its own constructor you are causing near infinite recursion:

  • your TopPanel constructor will create a new TopPanel object
    • whose constructor will create a new TopPanel object
      • whose constructor will create a new TopPanel object
        • whose constructor will create a new TopPanel object whose constructor ,....
          • ... etc -- recursion until your memory runs out. Don't do this.

Instead, don't do this:

public class TopPanel extends JPanel {

    public TopPanel() {     
        // get rid of the recursion
        // TopPanel tp = new TopPanel();

        // add the label to the current TopPanel object, the "this"
        add(new JLabel("Course Info"));

        // setSize(300,300); // generally not a good idea

        // tp.setVisible(true); // not needed
    }

    // this is an overly simplified method. You may wish to calculate what the 
    // preferred size should be, or simply don't set it and let the components
    // size themselves.
    public Dimension getPreferredSize() {
       return new Dimension(300, 300); 
    }

}

Edit
Also, while not in error, this:

   JPanel topPane = new TopPanel();
   JPanel topPanel = new JPanel();

is very confusing since the two JPanel variables are close enough in name to confuse us, and more importantly to confuse your future self. You will want to use more logical and distinct variable names.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top