java - 自定义 JButton 大小不正确?

标签 java swing jbutton gridbaglayout

我正在使用 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];    
    }
}

使用这段代码我得到了这样的结果: This is the result.

关于我在这里做错了什么有什么想法吗?

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

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

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

现在我一定是图标出了问题,因为它们没有按应有的方式显示。

最佳答案

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

关于java - 自定义 JButton 大小不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14946438/

相关文章:

java - 使用另一个方法中的数组元素

java - 记录通过套接字连接发送/接收的内容

java - 尝试在 Java 中返回分数(作为 float )

java - 当使用java 8 map、filter和collect时,它会创建新对象还是修改现有对象?

Java Swing 使用 defaulttablemodel 根据索引设置行和列的颜色

Java swing 风格的字体与 html 解析?

java - 如何禁用 JSplitPane 箭头按钮

java - 拆分数组列表以获取 URL 中的特定信息?

java - Swing 撤消选项

java - 更新 Java Swing 应用程序中的按钮状态