在JPanel中居中组件

时间:2013-10-27 23:14:31

标签: java swing

我正在尝试创建一个黑色杰克游戏,我需要对组件的布局提供一些帮助。布局看起来像这个atm。不要介意颜色。他们只是因为我希望能够区分不同的组件:

enter image description here

我正在尝试在蓝色JPanels中创建“卡片”。 “Spader 4”和“Spader 3”应该在中间彼此相邻产生,它们应该有一个边界。我怎样才能做到这一点。

    public KortlekSwing(){

    //Startar kortspelet
    kortlek.blandaKortlek();

    String playerKortEttText = drawCard(kortlek.Kortlek, playerScore);
    JLabel playerKortEtt = new JLabel(playerKortEttText);
    playerKortEtt.setSize(80, 45);
    playerKortEtt.setBackground(Color.WHITE);
    if(red){
        playerKortEtt.setForeground(Color.RED);
    } else {
        playerKortEtt.setForeground(Color.BLACK);
    }

    String playerKortTvåText = drawCard(kortlek.Kortlek, playerScore);
    JLabel playerKortTvå = new JLabel(playerKortTvåText);
    playerKortTvå.setSize(80, 45);
    playerKortTvå.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    playerKortTvå.setBackground(Color.WHITE);
    if(red){
        playerKortTvå.setForeground(Color.RED);
    } else {
        playerKortTvå.setForeground(Color.BLACK);
    }

    String computerKortEttText = drawCard(kortlek.Kortlek, computerScore);
    String computerKortTvåText = drawCard(kortlek.Kortlek, playerScore);

    JLabel computerKortEtt = new JLabel(computerKortEttText);
    JLabel computerKortTvå = new JLabel(computerKortTvåText);

    frame = new JPanel();
    frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));

    PlayerPanel = new JPanel();
    PlayerCardsPanel = new JPanel();
    PlayerButtons = new JPanel();

    ComputerPanel = new JPanel();
    ComputerCardsPanel = new JPanel();
    ComputerButtons = new JPanel();

    PlayerPanel.setLayout(new BorderLayout());
    PlayerCardsPanel.setLayout(new BorderLayout());

    ComputerPanel.setLayout(new BorderLayout());
    ComputerCardsPanel.setLayout(new BorderLayout());

    PlayerPanel.setSize(475, 120);
    PlayerCardsPanel.setSize(460, 120);
    PlayerButtons.setSize(460, 140);
    PlayerPanel.setBackground(Color.GREEN);
    PlayerCardsPanel.setBackground(Color.BLUE);
    PlayerButtons.setBackground(Color.CYAN);

    ComputerPanel.setSize(475, 120);
    ComputerCardsPanel.setSize(460, 120);
    ComputerButtons.setSize(460, 140);
    ComputerPanel.setBackground(Color.GREEN);
    ComputerCardsPanel.setBackground(Color.BLUE);
    ComputerButtons.setBackground(Color.CYAN);

    PlayerCardsPanel.add(playerKortEtt, BorderLayout.CENTER);
    PlayerCardsPanel.add(playerKortTvå, BorderLayout.CENTER);

    PlayerButtons.add(PlayerKortButton);
    PlayerButtons.add(PlayerPassButton);

    ComputerButtons.add(ComputerKortButton);
    ComputerButtons.add(ComputerPassButton);
    ComputerButtons.add(NyttSpelButton);
    ComputerButtons.add(ExitButton);

    PlayerText.setOpaque(false);
    PlayerPanel.add(PlayerText, BorderLayout.NORTH);
    PlayerPanel.add(PlayerCardsPanel, BorderLayout.CENTER);
    PlayerPanel.add(PlayerButtons, BorderLayout.SOUTH);

    ComputerText.setOpaque(false);
    ComputerPanel.add(ComputerText, BorderLayout.NORTH);
    ComputerPanel.add(ComputerCardsPanel, BorderLayout.CENTER);
    ComputerPanel.add(ComputerButtons, BorderLayout.SOUTH);

    ComputerPanel.setSize(475, 190);
    ComputerCardsPanel.setSize(460, 120);
    ComputerButtons.setSize(460, 100);

    NyttSpelButton.addActionListener(new NyttSpel());

    frame.add(PlayerPanel);
    frame.add(ComputerPanel);

    add(frame);
}

1 个答案:

答案 0 :(得分:2)

首先,不要在组件上使用setSize,这些值通常会被布局管理员忽略并更新。

其次,您可以尝试使用{{1},而不是在BorderLayoutComputerCardsPanel上使用PlayerCardsPanel,而FlowLayout似乎是显示卡片的位置}或GridBagLayout而不是

请查看A Visual Guide to Layout Managers了解更多提示