1

我正在尝试创建一个二十一点游戏,我需要一些关于组件布局的帮助。布局看起来像这个atm。不要介意颜色。它们就在那里,因为我希望能够区分不同的组件:

在此处输入图像描述

我正在尝试在蓝色 JPanel 中间创建“卡片”。“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);
}
4

1 回答 1

2

首先,不要setSize在组件上使用,这些值通常会被布局管理器忽略并在他们认为合适的时候更新。

BorderLayout其次,不要在ComputerCardsPaneland上使用 a PlayerCardsPanel,这似乎是要显示卡片的位置,您可以尝试使用 a FlowLayoutorGridBagLayout代替

查看A Visual Guide to Layout Managers以获得更多想法

于 2013-10-27T23:19:42.073 回答