1

The JPanel won't show on the JFrame and I have no idea why. It seems like the JPanel isn't being added to the JFrame somehow. Any suggestions?

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

public class LeaseItGUI extends JPanel{
    private int width=600,height=600;

    public void paintComponenet(Graphics g){
        super.paintComponent(g);
        g.fillRect(0, 0, width, height);
    }
}

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

public class LeaseItMain extends JFrame{
    private int width=600,height=600;

    public LeaseItMain(){
        setSize(width,height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        LeaseItGUI theGui = new LeaseItGUI();
        setVisible(true);
        add(theGui);
    }


    public static void main(String[] args){
        LeaseItMain LIM = new LeaseItMain();
    }
}
  • 1
    Add the panel before `setVisible` – Vince May 24 '16 at 13:26
  • 1
    Further to the advice of @VinceEmigh. The panel should return a preferred size of 600x600. Add the panel to the frame. Set the frame not resisizable, **before** calling.. **Pack** the frame. Set it visible. It will be the exact size needed to display the 600x600 panel, and will itself be larger than that (different sizes depending on OS etc.). – Andrew Thompson May 24 '16 at 13:41
  • I did, but to no avail none of the suggestions solved the problem... Something is weird with this situation. – Bobby C. Robillard May 24 '16 at 20:53

1 Answers1

2

Use:

add(theGui);

or

setContentPane(theGui);

before:

setVisible(true);
RobotKarel314
  • 417
  • 3
  • 14
  • I've done this as you can see in the main, I've tried your method but to no avail, this is a very weird situation as the panel simply won't show up on the JFrame – Bobby C. Robillard May 24 '16 at 20:52
  • 1
    It does work- you mispelled paintComponent() as paintComponenet(), so nothings getting painted on the panel. That doesn't mean the panel isn't there. – RobotKarel314 May 24 '16 at 21:05
  • Sometimes I wonder how I live on a day to day basis... Thank you very much for your assistance! – Bobby C. Robillard Jun 05 '16 at 15:31
  • What usually helps is to put `@Override` before a method that overrides a superclass/interface method. In this case, it would have given you a warning indicating that "paintComponenet" wasn't overriding anything. – RobotKarel314 Jun 05 '16 at 17:45