2013-01-09 8 views
1

我一直在研究一個應該模擬賭博遊戲的小項目。不幸的是,我在處理BoxLayout時遇到了一些奇怪的問題。就我所知,LayoutManager通常會兌現任何組件的首選尺寸。但是,在下面的代碼中,BoxLayout沒有。BoxLayout拒絕履行JButton的首選大小

這裏是我的代碼至今:

import java.awt.*; 
import javax.swing.*; 



public class Main 
{ 
    public static void main(String[] args) 
    { 
     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("Suit-Up"); 
     frame.setContentPane(makeGUI()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(900,450); 
     frame.setLocationRelativeTo(null); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 

    public static JPanel makeGUI() 
    { 
     JPanel main = new JPanel(); 
     main.setMinimumSize(new Dimension(900,450)); 
     main.setBackground(Color.red); 

     JPanel infoPanel = new JPanel(); 
     infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.LINE_AXIS)); 
     infoPanel.setPreferredSize(new Dimension(900,60)); 
     infoPanel.setBackground(Color.green); 
     main.add(infoPanel); 

     JPanel infoText = new JPanel(); 
     infoText.setLayout(new BoxLayout(infoText, BoxLayout.PAGE_AXIS)); 
     infoPanel.add(infoText); 

     JPanel moneyText = new JPanel(); 
     moneyText.setLayout(new BoxLayout(moneyText, BoxLayout.LINE_AXIS)); 
     infoText.add(moneyText); 

     JPanel lastGameText = new JPanel(); 
     lastGameText.setLayout(new BoxLayout(lastGameText, BoxLayout.LINE_AXIS)); 
     infoText.add(lastGameText); 

     JButton playAgain = new JButton("Play Again ($20)"); 
     playAgain.setPreferredSize(new Dimension(200,60)); 
     infoPanel.add(playAgain); 

     JButton finish = new JButton("End Session"); 
     finish.setPreferredSize(new Dimension(200,60)); 
     infoPanel.add(finish); 

     JPanel cardPanel = new JPanel(); 
     cardPanel.setLayout(new BoxLayout(cardPanel, BoxLayout.LINE_AXIS)); 
     main.add(cardPanel); 

     return main; 
    } 
} 

儘管指定首選大小兩個JButton S,它們不改變它們的大小。我也試過setMaximumSize()setMinimumSize(),但都沒有任何效果。

我可以忽略一些明顯的東西,或者這是BoxLayout的限制嗎?

+0

您可以隨時使用'BoxLayout',但嵌套組件... – mre

+0

對不起,但我不確定你的意思是嵌套。我對佈局經理來說比較陌生。 – Thrfoot

+0

如果將按鈕放在面板上並將面板直接添加到容器而不是按鈕,面板將被調整大小而不是按鈕。 – mre

回答

2

「據我所知,LayoutManagers通常會遵守任何組件的首選大小」 - 實際上並非如此。首選/最小/最大大小僅僅是佈局管理器可以用來確定如何最好地佈置其內容的「提示」。如果佈局經理想要,他們可以簡單地忽略它們。

從的JavaDoc

的BoxLayout 嘗試在它們的優選寬度 安排部件(對於水平佈局)或高度(對於垂直佈局)。對於 的水平佈局,如果不是所有組件的高度相同,則BoxLayout會嘗試使所有組件的組件高度最高爲 。如果某個特定組件無法實現,則根據 組件的Y對齊方式,BoxLayout垂直對齊該組件。默認情況下,組件的Y對齊爲 0.5,這意味着組件的垂直中心應該與其他組件的垂直中心具有相同的Y座標,且對齊爲0.5Y。

類似地,對於垂直佈局,BoxLayout會嘗試使列中的所有組件都與最寬的組件一樣寬。如果 失敗,它會根據它們的X路線水平對齊它們。 對於PAGE_AXIS佈局,基於組件前端的 完成水平對齊。換言之,如果容器的ComponentOrientation從左到右,則表示零件的左邊緣,否則表示零件的左邊緣,否則表示組件的右邊緣。

+0

雖然我使用的首選尺寸不應該有任何問題。 Theres有足夠的空間來適應'JPanel'中的'JButton',他們只是拒絕將其大小修改爲任何東西,只是在標籤中添加更多文本。我已閱讀文檔,但我沒有看到任何解釋我現在正在運行的內容。 – Thrfoot

+0

@Thrfoot我跑你測試代碼和'BoxLayout'只是拒絕使用'JButton'的'preferredSize',無論我嘗試了什麼。以這種方式操縱組件的首選大小通常是不可取的,通常鼓勵我們使用佈局管理器或直接覆蓋'getPreferredSize'方法。奇怪的是,'FlowLayout'工作得很好...... – MadProgrammer

+0

多數民衆贊成在奇怪......那麼,我想我將無法得到它與'BoxLayout'那麼工作。你會推薦任何其他佈局管理者,我可以得到一個或多或少類似的效果? – Thrfoot