1

我正在使用 Netbeans 创建一个匹配游戏,但不是 GUI 编辑器(它很烂)。因此,基本上,我创建了一个名为 Card 的新类,它扩展了 JButton 类。在构建时,按钮的大小设置为 100px x 100px 并设置了一个图标。当我将按钮添加到 GridBagLayout 中的 JPanel 时,它不是预期的大小。

这是我的一些代码:

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];    
    }
}

使用这段代码,我得到了这样的结果: 这就是结果。

关于我在这里做错了什么的任何想法?

编辑:我重写了 getPreferredSize 方法,就像 Hovercraft Full Of Eels 说的那样,它奏效了!我在 Card 类中添加了这段代码:

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

并得到了我想要的结果: 固定的!

现在我必须对图标做错了,因为它们没有按应有的方式显示。

4

1 回答 1

3

您不应setSize(...)在类的构造函数中使用,而应覆盖类的getPreferredSize()方法以返回 Dimension(100, 100)。实际上,您的程序中应该没有setSize(...) 任何位置。取而代之的是使用合适的布局管理器,pack()在添加所有组件之后调用 JFrame,然后再将其设置为可见,并让布局管理器适当地调整 GUI 的大小。

于 2013-02-18T22:26:51.317 回答