Question

I'm writing a game but i'm having a problem with my panels displaying. It seems to be almost all of them so I'm going to pick a specific one and cut and paste all relevant code. I'm trying to use gridbaglayout and card layout.

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

public class GridBadLayout extends JPanel
{
public GridBadLayout()
{
final GridBagLayout BAG = new GridBagLayout();              //layout for panels

    //buttons
JButton newB;               //button to play a new game
JButton quitB;              //button to quit the game
    //panels
JPanel menuP;               //hold menu buttons
JPanel messageP;            //card -- messages
    //constraints
GridBagConstraints conBag;  //set component constraints
CardLayout card;            //card layout
    //labels
JLabel newGameL;            //new game prompt message
JLabel roll1L;              //'1' rolled message
JLabel snakeEyesL;          //snake eyes rolled message
JLabel maxPointL;           //25+ points rolled

/*********** START ***************/


//instantiate grid constraints
conBag = new GridBagConstraints();

    //instantiate card objects
card = new CardLayout();

//instantiate panels and layouts
menuP = new JPanel(BAG);
messageP = new JPanel(card);

//instantiate buttons
newB = new JButton("New Game");
quitB = new JButton("Quit");

    //instantiate labels
newGameL = new JLabel("Welcome!! Enter your names and press 'PLAY'");
roll1L = new JLabel("You rolled a 1. Lose Your TURN!");
snakeEyesL = new JLabel("SNAKE EYES!! Lose Your POINTS!");
maxPointL = new JLabel("You have 25+ points! Next player's turn.");

//add cards         
        //message panel
card.addLayoutComponent(newGameL, "NewGame");
card.addLayoutComponent(roll1L, "OneRolled");
card.addLayoutComponent(snakeEyesL, "SnakeEyes");
card.addLayoutComponent(maxPointL,  "MaxPoints");

    //build main panels
    //menu 
            //constraints for new game button
    conBag.gridx = 0;
    conBag.gridy = 0;
    conBag.gridwidth = 2;
    conBag.gridheight = 2;
    conBag.weightx = .15;
    conBag.fill = GridBagConstraints.BOTH;
menuP.add(newB,conBag);
    //constraints for message panel
    conBag.gridx = 2;
    conBag.gridy = 0;
    conBag.gridwidth = 8;
    conBag.gridheight = 2;
    conBag.weightx = .7;
    conBag.fill = GridBagConstraints.BOTH;
menuP.add(messageP);
    //constraints for quit button
    conBag.gridx = 10;
    conBag.gridy = 0;
    conBag.gridwidth = 2;
    conBag.gridheight = 3;
    conBag.weightx = .15;
    conBag.fill = GridBagConstraints.BOTH;
menuP.add(quitB,conBag);


   /*********************************
  This next line shouldn't be necessary because it should auto load the first card.
  Just putting it in here in case that is someone's first thought on the issue.
  ***********************************/

    card.show(messageP, "NewGame");  
add(menuP);

The two buttons show scrunched next to each other. But with the GridBagLayout and the constraints set there should be a large gap in between them which is where the message panel should fit in.

The full program is much longer this is a copy and paste of every part that will show the problem. This code will compile and run as is.

Was it helpful?

Solution

I think you will find this code example on CardLayout very helpful. The problem you were having is instead of using:

card.addLayoutComponent(newGameL, "NewGame");
card.addLayoutComponent(roll1L, "OneRolled");
card.addLayoutComponent(snakeEyesL, "SnakeEyes");
card.addLayoutComponent(maxPointL,  "MaxPoints");

you should use:

messageP.add(newGameL,"NewGame");
messageP.add(roll1L,"OneRolled");
messageP.add(snakeEyesL,"SnakeEyes");
messageP.add(maxPointL,"MaxPoints");

then you use:

card.show(messageP,"MaxPoints");

to switch to the appropriate JLabel.

or you can use:

card.next(messageP);

to rotate through the values.

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