2017-06-23 39 views
2

GUIGUI框佈局Y軸不內嵌

我設計該GUI的整體框架使用的BorderLayout,則有3塊板,驚奇板面板使用GridLayout的,在輸入詞實測值面板(由JTextArea,Time left JLabel和Shake Dice JButton)使用帶有Y軸的BoxLayout,當前字面板(包括當前字JLabel,提交字JButton和Score JLabel)正在使用FlowLayout。

正如你所看到的,JTextArea不是沿着Y軸與內置的JLabel和Shake Dice JButton一起內聯的,我不知道爲什麼會發生這種情況。如果有人能夠闡明爲什麼他們不合格,那將是非常好的謝謝。

,這裏是我的輸入幾個詞的JPanel代碼:

// Adding words found JPanel 
     wordFoundPanel = new JPanel(); 
     wordFoundPanel.setLayout(new BoxLayout(wordFoundPanel, BoxLayout.Y_AXIS)); 
     wordFoundPanel.setBorder(new TitledBorder("Enter Words Found")); 

     wordArea = new JTextArea(); 
     wordArea.setLineWrap(true); 
     wordArea.setWrapStyleWord(rootPaneCheckingEnabled); 

     // Adding JScrollPane to the wordArea 
     scroll = new JScrollPane(wordArea); 
     scroll.setMinimumSize(new Dimension(200, 180)); 
     scroll.setPreferredSize(new Dimension(200, 180)); 
     scroll.setMaximumSize(new Dimension(200, 180)); 
     scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 

     wordFoundPanel.add(scroll); 

     // Adding Clock Label 
     clockLabel = new JLabel("3:00", CENTER); 
     clockLabel.setMinimumSize(new Dimension(200, 100)); 
     clockLabel.setPreferredSize(new Dimension(200, 100)); 
     clockLabel.setMaximumSize(new Dimension(200, 100)); 
     clockLabel.setFont(new Font("Tahoma", Font.BOLD, 30)); 
     clockLabel.setBorder(new TitledBorder("Time Left")); 

     wordFoundPanel.add(clockLabel); 

     // Adding Shake Dice button 
     shakeDiceButton = new JButton("Shake Dice"); 
     shakeDiceButton.setMinimumSize(new Dimension(200, 100)); 
     shakeDiceButton.setPreferredSize(new Dimension(200, 100)); 
     shakeDiceButton.setMaximumSize(new Dimension(200, 100)); 

     wordFoundPanel.add(shakeDiceButton); 

     frame.add(wordFoundPanel, BorderLayout.EAST); 
+1

1)爲了更好地幫助越早,張貼[MCVE]或[簡要,獨立的,正確的示例](http://www.sscce.org/)。 2)以最小尺寸提供ASCII藝術或簡單的GUI圖形*圖形,並且如果可調整大小,則具有更大的寬度和高度。 3)請參閱[我是否應避免使用Java Swing中的set(Preferred | Maximum | Minimum)Size方法?](http://stackoverflow.com/q/7229226/418556)(是)。 –

回答

3

How to Use BoxLayout取向固定問題節中,我們可以讀到:

在一般情況下,所有的組件控制由上到下BoxLayout 對象應具有相同的X對齊

因此,只需在您的三個組件上調用例如setAlignmentX(Component.RIGHT_ALIGNMENT);,它就可以修復這個奇怪的顯示。

enter image description here