2013-02-18 50 views
1

我正在使用Netbeans創建一個匹配遊戲,但不是GUI編輯器(它很糟糕)。所以,基本上,我創建了一個名爲Card的新類,它擴展了JButton類。在構建時,按鈕的大小設置爲100px×100px,並設置圖標。當我將按鈕添加到GridBagLayout中的JPanel時,它不是預期的大小。自定義JButton沒有正確調整大小?

下面是我的一些代碼:

JFrame類:

package matchinggame; 

... imports ... 

public class MatchingGameWindow extends JFrame { 

    Card[] cards = new Card[16]; //16 game cards 

    public MatchingGameWindow() { 
     ... 
     //Create new game panel (for the cards) 
     JPanel gamePanel = new JPanel(new GridBagLayout()); 
     //gamePanel.setSize(500,500); removed as it is not needed. 
     ... 
     this.add(gamePanel, BorderLayout.CENTER); 
     //Create 16 card objects 
     cards = createCards(); 

     //Create new grid bag constraints 
     GridBagConstraints gbc = new GridBagConstraints(); 

     //Add the cards to the game panel 
     int i=0; 
     for (int y = 0; y < 4; y++) { 
      gbc.gridy = y; 
      for (int x = 0; x < 4; x++) { 
       gbc.gridx = x; 
       gamePanel.add(cards[i], gbc); 
       i++; 
      } 
     } 
    } 

    public final Card[] createCards() { 
     Card[] newCards = new Card[16]; 
     //New choices array 
     ArrayList<Integer> choices = new ArrayList(); 
     int[] choiceValues = {0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7}; 
     //Add the initial choice values to the arraylist 
     for (int i=0; i < choiceValues.length; i++) { 
      choices.add(choiceValues[i]); 
     } 

     //Create 16 cards 
     for (int i=0; i < 16; i++) { 
      //Initial value of -1 for error checking 
      int iconIndex = -1; 
      //Loop until card is created 
      while (iconIndex == -1) {  
       //Get a random number from 0 - 7 
       Random r = new Random(); 
       int rInt = r.nextInt(8); 
       //If the random number is one of the choices 
       if (choices.contains(rInt)) { 
        //the icon # will be the random number 
        iconIndex = rInt; 
        //Get rid of that choice (it is used up) 
        choices.remove(new Integer(rInt)); 
        //Create a new Card in the Card[] 
        newCards[i] = new Card(i,iconIndex); 
       //If all the choices are gone 
       } else if (choices.isEmpty()){ 
        iconIndex = -1; //done creating this card (breaks out of loop) 
       } 
      } 
     } 
     //Return the created cards 
     return newCards; 
    } 
} 

卡類:

package matchinggame; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 

public class Card extends JButton { 

    final static ImageIcon defaultIcon = new ImageIcon("cardback.jpg"); 
    ImageIcon secretIcon; 
    boolean isRevealed = false; 

    ... 

    public Card(final int cardIndex, int secretIconIndex) { 
     //Size is 100px by 100px    
     setSize(100, 100); 
     //Default icon is card back image 
     setIcon(defaultIcon); 
     //Get the secret icon behind the back of the card 
     secretIcon = icons[secretIconIndex];  
    } 
} 

並使用此代碼,我得到這樣的結果: This is the result.

任何想法,我在做什麼錯在這裏?

編輯: 我重寫了像Hovercraft Full of Eels的getPreferredSize方法,它工作! 我加入這個代碼的卡類:

@Override 
public Dimension getPreferredSize() { 
    return new Dimension(100,100); 
} 

,並得到了我想要的結果: Fixed!

現在我必須做一些錯誤的圖標,因爲它們不是因爲他們應該顯示出來。

回答

3

您不應該在類的構造函數中使用setSize(...),而是覆蓋該類的getPreferredSize()方法以返回Dimension(100,100)。而實際上你應該有setSize(...)no-where在你的程序中。相反,使用合適的佈局管理器,在添加所有組件並將其設置爲可見之前,請在JFrame上調用pack(),並讓佈局管理器適當調整GUI大小。

+0

工作!謝謝! – jessechk 2013-02-18 22:41:02

+0

@Jaybob:不客氣! – 2013-02-18 22:42:36