1

几年后,我一直在制作一个愚蠢的小卡片技巧来重新使用 Java,并且在让 JPanel 去我想要的地方时遇到了一些困难。

给我带来麻烦的组件是 selectedCard 和 label。其他一切都很好地位于中心。每个 cardRow JPanel 包含 7 张卡片,而 mainRow# 包含 cardRow 旁边有一个按钮。每张卡都是 74x98,所以我尽量保持所有尺寸。

这是处理所有展示位置和布局的 Trick 类。

public Trick()
{
    setMaximumSize(new Dimension(650, 600)); //set the size large enough to hold all panels neatly

    //Card row setup
    trickCards = new Deck().getCards(21);
    cardRow1 = new JPanel();
    cardRow2 = new JPanel();
    cardRow3 = new JPanel();

    cardRow1.setPreferredSize(new Dimension(650, 120));
    cardRow2.setPreferredSize(new Dimension(650, 120));
    cardRow3.setPreferredSize(new Dimension(650, 120));
    cardRow1.setLayout(new BoxLayout(cardRow1, BoxLayout.X_AXIS));
    cardRow2.setLayout(new BoxLayout(cardRow2, BoxLayout.X_AXIS));
    cardRow3.setLayout(new BoxLayout(cardRow3, BoxLayout.X_AXIS));
    setToRows();

    button1 = new JButton("Row 1");
    button1.setMnemonic(KeyEvent.VK_1);
    button1.setActionCommand("1");
    button2 = new JButton("Row 2");
    button2.setMnemonic(KeyEvent.VK_2);
    button2.setActionCommand("2");
    button3 = new JButton("Row 3");
    button3.setMnemonic(KeyEvent.VK_3);
    button3.setActionCommand("3");

    group = new ButtonGroup();
    group.add(button1);
    group.add(button2);
    group.add(button3);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);

    chosenCard = new JPanel();
    chosenCard.setLayout(new BoxLayout(chosenCard, BoxLayout.Y_AXIS));
    chosenCard.setPreferredSize(new Dimension(100, 120));
    chosenCard.add(new Card(trickCards[1].getCardFace(), -1, -1));

    label = new JLabel("Card Trick! Pick any one card and click the row it is in [1, 2, 3]");
    label.setPreferredSize(new Dimension(200, 20));

    mainRow1 = new JPanel();
    mainRow2 = new JPanel();
    mainRow3 = new JPanel();
    mainRow1.setPreferredSize(new Dimension(750, 120));
    mainRow1.add(cardRow1);
    mainRow1.add(button1);
    mainRow2.setPreferredSize(new Dimension(750, 120));
    mainRow2.add(cardRow2);
    mainRow2.add(button2);
    mainRow3.setPreferredSize(new Dimension(750, 120));
    mainRow3.add(cardRow3);
    mainRow3.add(button3);

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    add(mainRow1);
    add(mainRow2);  
    add(mainRow3);
    add(label);
    add(chosenCard);
}

//Refreshes what cards are in the rows
//Usually done after a selection is made

private void setToRows()
{
    cardRow1.removeAll();
    cardRow2.removeAll();
    cardRow3.removeAll();
    for(int x = 0; x < trickCards.length / 3; x++)
    {
        cardRow1.add(trickCards[x]);
        cardRow2.add(trickCards[x + (trickCards.length / 3)]);
        cardRow3.add(trickCards[x + ((trickCards.length / 3) * 2)]);
    }
}

//The listener for the buttons
//Will be expanded
public void actionPerformed(ActionEvent e)
{
    if(e.getActionCommand().equals("1"))
    {
        System.out.println("1");
        label.setText("You chose 1!");
    }
    else if(e.getActionCommand().equals("2"))
    {
        System.out.println("2");
    }
    else if(e.getActionCommand().equals("3"))
    {
        System.out.println("3");
    }
    else
        System.out.println("There's something wrong with the choice! Set to 0");
}
4

1 回答 1

0

我猜你的标签是居中的,但标签中的文字不是。

所以将文本对齐设置为 CENTER

label.setHorizontalAlignment(JLabel.CENTER);
于 2013-08-23T23:07:44.000 回答