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

时间:2013-02-18 22:25:38

标签: java swing jbutton gridbaglayout

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

以下是我的一些代码:

JFRAME CLASS:

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

CARD CLASS:

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方法,并且它有效! 我在Card类中添加了这段代码:

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

得到了我想要的结果: Fixed!

现在我必须对图标做错事,因为它们没有显示出来。

1 个答案:

答案 0 :(得分:3)

您不应在类的构造函数中使用setSize(...),而应覆盖类的getPreferredSize()方法以返回Dimension(100,100)。事实上,你的程序中应该有setSize(...) no-where 。而是使用体面的布局管理器,在添加所有组件之后,在设置它之前调用JFrame上的pack(),并让布局管理器适当地调整GUI的大小。