GridBag /面板显示

时间:2014-05-08 16:51:13

标签: java swing jpanel gridbaglayout cardlayout

我正在写游戏,但我的面板显示有问题。它似乎几乎全部,所以我将选择一个特定的,并剪切并粘贴所有相关的代码。我正在尝试使用gridbaglayout和卡布局。

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);

两个按钮显示彼此相邻。但是使用GridBagLayout和设置的约束,它们之间应该存在很大的差距,这是消息面板应该适合的位置。

完整的程序要长得多,这是复制和粘贴每个可以显示问题的部分。此代码将按原样编译和运行。

1 个答案:

答案 0 :(得分:1)

我认为您会在CardLayout上找到此代码示例非常有帮助。您遇到的问题不是使用:

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

你应该使用:

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

然后你使用:

card.show(messageP,"MaxPoints");

切换到相应的JLabel。

或者您可以使用:

card.next(messageP);

旋转值。