gpt4 book ai didi

java - 将 JPanel 内的组件居中

转载 作者:行者123 更新时间:2023-11-30 04:10:54 24 4
gpt4 key购买 nike

我正在尝试创建一个黑 jack 游戏,并且我需要一些有关组件布局的帮助。布局看起来就像这个自动取款机。不要介意颜色。它们之所以存在,是因为我希望能够区分不同的组件:

enter image description here

我正在尝试在蓝色 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);
}

最佳答案

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

其次,不要在 ComputerCardsPanelPlayerCardsPanel 上使用 BorderLayout(这似乎是显示卡片的位置),您可以尝试使用 FlowLayoutGridBagLayout 代替

看看A Visual Guide to Layout Managers获取更多想法

关于java - 将 JPanel 内的组件居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19624822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com