2014-05-08 68 views
1

我在寫遊戲,但是我的面板顯示出現問題。它似乎幾乎都是它們,所以我要選擇一個特定的代碼並剪切並粘貼所有相關的代碼。我正在嘗試使用gridbaglayout和卡布局。GridBag /面板顯示

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

這兩個按鈕顯示彼此相鄰的scrunched。但是隨着GridBagLayout和約束的設置,它們之間應該有一個很大的差距,這是消息面板應該適合的地方。

完整的程序要長得多,這是複製和粘貼每個部分,將顯示問題。此代碼將按原樣編譯和運行。

+0

看起來你不添加conBag當你添加messageP菜單P – lgraham076

回答

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

通過值旋轉。

+0

謝謝。在讀入Oracle時,cardLayoutComponent讓我感到困惑。優秀的答案。 – wizeman02