java - 使用 BoxLayout 时 JPanel 不居中

标签 java swing user-interface jpanel layout-manager

几年后,我一直在制作一个愚蠢的小纸牌戏法来重新回到 Java,但在让 JPanel 到达我想要的位置方面我遇到了一些困难。

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

最佳答案

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

因此将文本对齐方式设置为居中

label.setHorizontalAlignment(JLabel.CENTER);

关于java - 使用 BoxLayout 时 JPanel 不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18403949/

相关文章:

java - 标签未显示在面板上(已尝试重新绘制和验证)

java - 如何保护 libgdx 函数 fromJson 免受错误输入的影响?

java - 依次使用不同的请求正文发出多个 API 请求

java - 线程 "UI Thread"java.lang.UnsatisfiedLinkError : 中出现异常

java - 表中已筛选行的工具提示

java - java GUI 中这些被阻止的部分是什么

java - 如何诊断泄漏的 http 连接 (org.apache.http.impl.conn.tsccm.ConnPoolByRoute)

java - JPanel组件绘制顺序

java - 仅允许 JTextfield 中的数字

user-interface - 创建调用 OCaml 或 Haskell 的 GUI 桌面应用程序——这是傻事吗?