0

so today's question on my mind is this. I'm trying to develop a trivia game and to do so I need to add 2 JPanels to the screen. The problem, only one shows up, specifically the first one initialized. I checked out some other similar questions on this site but to no avail. Any ideas on how to fix this? questionPanel and anotherPanel both are classes that extend JPanel. Why won't both show up at the same time?

 import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Trivia extends JFrame{

    questionPanel qp;
    private JButton q1,q2,q3,q4;

    public Trivia(){
        setSize(600,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        qp = new questionPanel();
        add(qp,BorderLayout.SOUTH);

        anotherPanel ap = new anotherPanel();
        add(ap,BorderLayout.NORTH);


    }

    public static void main(String args[]){
        Trivia t = new Trivia();
    }

}
  • 1
    Call `setVisible(true)` after you have added both panels. Also, you could wrap your `Trivia t = new Trivia()` inside a `EventQueue.invokeLater(new Runnable() {public void run() {Trivia t = new Trivia()}});`. If that doesn't help, please provide a [mcve] as we also don't know what contents / preferred sizes the panels have. – Lukas Rotter Apr 25 '16 at 18:03
  • 2
    `so today's question on my mind is this` - and my question is why don't you ever "accept" answers in your other questions when you get help? I think I'll skip this one. – camickr Apr 25 '16 at 18:23
  • I accept answers if they're right for one, second no reason to be snappy, this is supposed to be a professional environment. If you don't like my methods go waste your time elsewhere sir/madam. – Bobby C. Robillard Apr 25 '16 at 23:35
  • @LuxxMiner that doesn't do anything. JPanels are visible by default, knowing that I still tried your suggestion and the program still failed to work. – Bobby C. Robillard Apr 25 '16 at 23:38
  • @BobbyC.Robillard, `this is supposed to be a professional environment.` - exactly, which is why you should be "accepting" answers when you get help, OR respond to every answer stating it didn't help. In other words you acknowledge the effort people make. You have way too many questions asked without being accepted. If the answers don't answer your question then the problem is with your question. You will not always get answers that give you 100% of the code. Even if the answer POINTs you in the right direction the answer should be accepted. – camickr Apr 26 '16 at 00:13
  • 1
    You have also, been asked to post a MCVE, which you have not done yet. The problem is probably with the code you have not included. Again, in a professional environment you listen to suggestions made by people trying to help. We need ALL the information in order to make a suggestion otherwise it is just a guess. And by the way the answer given here and in the "duplicate" question is the best anybody can do based on the information provided. – camickr Apr 26 '16 at 00:17

1 Answers1

3

Call setVisible(true); at the end of the Trivia constructor

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
  • That didn't change anything, all setVisible does is changed the visibility of the entire JFrame. – Bobby C. Robillard Apr 25 '16 at 23:36
  • 1
    @BobbyC.Robillard, `setVisible()` does more than that. It invokes the layout manager so components can be give a size. Otherwise components have a size of (0, 0) so there is nothing to paint. That is why resizing the frame works, the layout manager is invoked. – camickr Apr 26 '16 at 00:19