我正在尝试创建一个二十一点游戏,我需要一些关于组件布局的帮助。布局看起来像这个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);
}