0

I know this question has been asked a lot and I have done my research but still can not find anything. Below is proof of this before anyone gets upset:

I found this link: https://coderanch.com/t/563764/java/Blank-Frame-Panel

and this: Adding panels to frame, but not showing when app is run

and this: Why shouldn't I call setVisible(true) before adding components?

and this: JPanel not showing in JFrame?

But the first question says use repaint which I tried with no fix and the second and third to last questions talk about putting setVisible after components added which I have.

The last one talks about making the users JPanelArt a extension (done so) of JPanel instead of JFrame and not to make a frame in a frame constructor (also have not done)

When ever I run this I just get a blank frame, as if the panel was never added in.

I apologise if I have missed something in those links. Anyway below is my classes:

GUI.java (extends JFrame)

package javaapplication2;

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

public class GUI extends JFrame{

public GUI(String name) {
    super(name);

    getContentPane().setLayout(null);

    JPanel myPanel1 = new GUIPanel();
    myPanel1.setLocation(20, 20);
    getContentPane().add(myPanel1);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();
    setResizable(true);
}

public static void main(String[] args) {
    JFrame frame = new GUI("Game");
    frame.setVisible(true);
}
}

GUIPanel.java (extends JPanel)

package javaapplication2;

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


public class GUIPanel extends JPanel {

JButton start;
JButton inspect1;
JButton inspect2;
JButton inspect3;
JButton suspect;

public GUIPanel() {
    setLayout(new BorderLayout());

    start = new JButton("Start Game");
    inspect1 = new JButton("Inspect 1");
    inspect2 = new JButton("Inspect 2");
    inspect3 = new JButton("Inspect 3");
    suspect = new JButton("Choose Suspect");

    add(start, BorderLayout.WEST);
    add(inspect1, BorderLayout.WEST);
    add(inspect2, BorderLayout.WEST);
    add(inspect3, BorderLayout.WEST);
    add(suspect, BorderLayout.WEST);

}
}

I know it is very simple, but that is because I am following a tutorial from my lecturer to get the hang of things as I previously used a GUI builder which someone in this community pointed out to me is not good to start with (very correct!)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DaleJV
  • 370
  • 1
  • 3
  • 15
  • Why are you declaring GUIPanel as an instance of the super class instead of what it actually is? `JPanel myPanel1 = new GUIPanel();` – MarsAtomic May 21 '20 at 04:43
  • This was how it was done in our tutorial at my university. Is it better to just do GUIPanel myPanel1 = new GUIPanel()? I originally did that but ended up changing it because I saw the tutorial. – DaleJV May 21 '20 at 06:28

1 Answers1

2

The issue lies in your GUI class when you call getContentPane().setLayout(null). Because of this method call, your JFrame is not displaying anything. If you remove it, your elements should show up.

I also noticed that you were setting each JButton to have a constraint of BorderLayout.WEST. This will most likely put your JButtons on top of each other and render only one of them.

parthlr
  • 386
  • 3
  • 13
  • I see you are a new contributor so first of thank you for helping the community! That fixed my issue, thank you for that. Also yes it looks like they have stacked on top of each other. how do I change this? I assume by setting a size for each button? – DaleJV May 21 '20 at 06:33
  • @ExileVoid No problem :) To fix your button issue, you're going to have to look into how to utilize the different layout managers for GUI components. The [JavaDocs](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) has a list of all of them and example code of how to use them and what the result of it will be. Generally, you want to pick only one that best suits how you want your GUI to be laid out. – parthlr May 21 '20 at 17:31