自定义JButton没有正确调整大小?

问题描述:

我正在使用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!

现在我必须做一些错误的图标,因为它们不是因为他们应该显示出来。

您不应该在类的构造函数中使用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