2013-10-27 56 views
1

我正在嘗試創建一個黑色的插孔遊戲,並且我需要一些組件佈局的幫助。佈局看起來像這個atm。不介意顏色。他們只是有監守我希望能夠區分不同的組件:JPanel內部的對中組件

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

爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

2

首先,不使用的組件setSize,這些值通常由佈局管理器,因爲他們認爲合適的忽略和更新。

其次,而是採用對ComputerCardsPanelPlayerCardsPanel,這似乎是在那裏將被顯示在卡BorderLayout使用,你可以嘗試使用FlowLayoutGridBagLayout代替

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

+0

好的,謝謝。 – user1924247