2013-02-12 42 views
5

這是我的佈局。Swing中的FlowLayout

enter image description here

兩個單選按鈕應該是下面的歡迎標籤。

這樣的:

__________________________ 
|      | 
|  WELCOME   | 
|   * *   | 
|      | 
|      | 
|      | 
|________________________| 

兩個星號是單選按鈕。

我的代碼:

northpanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 
northpanel1.setLayout(new FlowLayout(FlowLayout.CENTER)); 


northpanel.add(welcome); //this welcome text label 

northpanel1.add(r1); //this radio 1 
northpanel1.add(r2); //this radio 2 


add(northpanel,BorderLayout.NORTH); 
add(northpanel1,BorderLayout.NORTH); 
+0

你的問題是明確? – 2013-02-12 17:07:08

+0

Hpw獲取WELCOME標籤下方的兩個單選按鈕 – 2013-02-12 17:07:59

+0

解決方案:請勿使用FlowLayout。使用BorderLayout作爲主容器,然後中央容器應該使用GridLayout來容納JRadioButton。 – 2013-02-12 17:10:56

回答

3

添加northpanel並具有GridLayout(0, 1)northpanelpanel然後

add(panel, BorderLayout.NORTH); 
+0

然而,歡迎標籤被推到屏幕上最遠的地方,但是2個單選按鈕是根據需要集中的 – 2013-02-12 17:20:20

+0

@ user2016977:嘗試使用FlowLayout。 CENTRE'作爲SoboLAN [顯示](http://stackoverflow.com/a/14838145/230513)。 – trashgod 2013-02-12 21:40:13

2

可以超過一個組件添加到BorderLayout區域和你到底在做什麼吧。你需要改變你的northpanel是一個BorderLayout,然後把你的歡迎文本和northtestpanel1裏面,像這樣:

northpanel -> BorderLayout, JFrame's NORTH position 
welcome -> northpanel NORTH position 
northpanel1 -> FlowLayout, northpanel CENTER position 

你可能有問題,關於在中心放welcome(我只是猜測,也許它將工作正常)。如果您沒有任何解決方案,只需將其包裝到一個新的JPanel中,然後使用FlowLayoutFlowLayout.CENTER

1

你必須使用網格佈局OR GridBagLayout的,而不是流量,layout.First設置northpanel的GridBagLayout中,然後添加你需要的組件,讓說你的收音機按鈕和惠康標籤。欲瞭解更多詳情,請諮詢here

5
import javax.swing.*; 
import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 

public class StackOverflow14837740 
{ 
    public static void main (String[] args) 
    { 
     SwingUtilities.invokeLater (new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame(); 
     frame.setLayout (new BorderLayout()); 
     frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); 

     JPanel northPanel = new JPanel (new GridLayout (2, 1)); 

     JPanel welcomePanel = new JPanel (new FlowLayout (FlowLayout.CENTER));  
     welcomePanel.add (new JLabel ("Welcome")); 

     northPanel.add (welcomePanel); 

     JPanel radioPanel = new JPanel (new FlowLayout (FlowLayout.CENTER)); 

     JRadioButton button1 = new JRadioButton ("Button 1", true); 
     JRadioButton button2 = new JRadioButton ("Button 2", false); 

     ButtonGroup group = new ButtonGroup(); 
     group.add (button1); 
     group.add (button2); 

     radioPanel.add (button1); 
     radioPanel.add (button2); 

     northPanel.add (radioPanel); 

     JPanel middlePanel = new JPanel (new GridLayout (3, 3)); 

     for (int i = 0; i < 3; i++) 
     { 
      for (int j = 0; j < 3; j++) 
      { 
       middlePanel.add (new JButton ("Button " + i + j)); 
      } 
     } 

     JPanel southPanel = new JPanel (new FlowLayout (FlowLayout.CENTER)); 

     southPanel.add (new JLabel ("Whose turn:")); 
     southPanel.add (new JButton ("Reset")); 

     frame.add (northPanel, BorderLayout.NORTH); 
     frame.add (middlePanel, BorderLayout.CENTER); 
     frame.add (southPanel, BorderLayout.SOUTH); 

     frame.pack(); 
     frame.setVisible (true); 
    } 
} 

它看起來像這樣(雖然你必須調整它一下):

printscreen

+0

哇..非常感謝你..我真的很驚訝這個社區的答案!保持良好的工作! :) – 2013-02-12 18:17:26

+1

@ user2016977這是因爲社區是巨大的(數十萬人)......哦,因爲我們很棒:D。用於'FlowLayout.CENTER'的 – 2013-02-12 18:35:29

+0

+1。 – trashgod 2013-02-12 21:40:40