使用BoxLayout时,JPanels不居中

时间:2013-08-23 13:21:42

标签: java swing user-interface jpanel layout-manager

几年后,我一直在制作一个愚蠢的小卡片技巧,以便重新使用Java,而且我一直在努力让JPanels进入我想要的地方。

给我带来麻烦的组件选择卡和标签。其他一切都很好地位于中心。每个cardRow JPanels持有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");
}

1 个答案:

答案 0 :(得分:0)

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

所以将文本对齐设置为CENTER

label.setHorizontalAlignment(JLabel.CENTER);