-1

For some reason I am unable to add my secondary JPanel under the first one that I have. I am able to add the first JPanel with no errors, however when I try to add the second JPanel in the same way it does not work, and gives me a multitude of errors. I am not sure exactly what it is, but I am thinking it might either be how I am trying to put it into the frame, or because of the JFrame's layout. Here is the code for the secondary layout:

    private MazeModel model;

public UIPanel(){
    setPreferredSize(new Dimension(100, 100));
    setBackground(Color.WHITE);
}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawString("Moves taken: " + model.getMoveCounter(), 10, 0);
    g.drawString("Current location: " + model.getPlayerX() + ", " + model.getPlayerY(), 10, 50);
}

Here is the frame here I am trying to add both the first main panel, as well as this new second one that is giving me trouble:

    MazeView(){ //creating the JFrame and JPanel
    model = new MazeModel();
    panel = new MazePanel(model); //panel
    player = new Player(model); //player class
    //uipanel = new UIPanel();
    controller = new MazeController(panel, model); //movement

    this.setLayout(new BorderLayout()); //non null BorderLayout
    this.setPreferredSize(new Dimension(500, 600));
    addKeyListener(controller);
    add(panel, BorderLayout.NORTH);
    add(uipanel, BorderLayout.SOUTH);
    this.pack();

I hope that I am providing enough code to help understand the issue I am having, if not just let me know. I still think it is something with the way I am adding it, and not the setup of the JPanel itself, but I could be wrong.

user1234
  • 17
  • 4
  • You do not provide enough information such as exceptions, there is no way to tell what you are really asking. What you mean by errors, exceptions are thrown? Stacktrances print out? Is your panel just not visible? – Derrops May 08 '15 at 06:00

2 Answers2

1

Override the getPreferredSize method of UIPanel so the BorderLayout has some idea what size you might like the panel to be, otherwise it will size to its default size of 0x0

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
 //uipanel = new UIPanel();

this could be your problem, uipanel is null or unknown.

Do you have any Exceptions ?

If you want to make Mazeview a JFrame you should extend it from JFrame.

public class MazeView extends JFrame{
...
}
Gordon
  • 69
  • 4