2012-05-22 24 views
2

我有一個Swing窗體,它包含一個JPanel的JScrollPane(activityScrollPane)(activityPanel)。該面板包含一個JTextField和一個JButton(用於向面板添加更多字段)。現在的問題是,元素從面板的中心開始,如下圖(標有邊界標誌着activityScrollPane邊界)是否可以使用GridBagLayout使JPanel內的元素從左上角開始?

enter image description here

以下是我目前正在使用,使滾動窗格中的代碼和相關的組件。

//part of the code for creating the ScrollPane 

     final JPanel activityPanel = new JPanel(new GridBagLayout()); 
     gbc.gridx=0; 
     gbc.gridy=0; 
     JScrollPane activityScrollPane = new JScrollPane(activityPanel); 
     //adding activity fields 
     activityFields = new ArrayList<JTextField>(); 
     fieldIndex = 0; 
     activityFields.add(new JTextField(30)); 

     final GridBagConstraints activityGBC = new GridBagConstraints(); 
     activityGBC.gridx=0; 
     activityGBC.gridy=0; 
     activityGBC.anchor = GridBagConstraints.FIRST_LINE_START; 
     activityPanel.add(activityFields.get(fieldIndex),activityGBC); 

     fieldIndex++; 
     JButton btn_more = (new JButton("more")); 
     activityGBC.gridx=1; 
     activityPanel.add(btn_more,activityGBC); 

如何讓JTextField的和將JButton或與此有關的任何組件出現在JScrollPane中的左上角。使用

activityConstraints.anchor = GridBagConstraints.NORTHWEST; 

如指出的SO post我已經試過了,但它並沒有在所有似乎工作。

回答

3

只需忘記提供任何weightx/weighty值,至少一個具有非零值都行。看看這個代碼示例:

import java.awt.*; 
import javax.swing.*; 
public class GridBagLayoutDemo 
{ 
    private JTextField tfield1; 
    private JButton button1; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("GridBagLayout Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridBagLayout()); 

     tfield1 = new JTextField(10); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.HORIZONTAL;  
     contentPane.add(tfield1, gbc); 

     button1 = new JButton("More"); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.NONE; 
     contentPane.add(button1, gbc); 

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

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new GridBagLayoutDemo().displayGUI(); 
      } 
     }); 
    } 
} 

最新編輯:無間距沿Y軸

import java.awt.*; 
import javax.swing.*; 
public class GridBagLayoutDemo 
{ 
    private JTextField tfield1; 
    private JButton button1; 
    private JTextField tfield2; 
    private JButton button2; 

    private void displayGUI() 
    { 
     JFrame frame = new JFrame("GridBagLayout Demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setLayout(new GridBagLayout()); 

     tfield1 = new JTextField(10); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
     gbc.weightx = 1.0; 
     //gbc.weighty = 0.2;  
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.HORIZONTAL;  
     contentPane.add(tfield1, gbc); 

     button1 = new JButton("More"); 
     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.fill = GridBagConstraints.NONE; 
     contentPane.add(button1, gbc); 

     tfield2 = new JTextField(10); 
     gbc.weightx = 1.0; 
     gbc.weighty = 0.2; 
     gbc.gridx = 0; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.HORIZONTAL;  
     contentPane.add(tfield2, gbc); 

     button2 = new JButton("More"); 
     gbc.gridx = 1; 
     gbc.gridy = 1; 
     gbc.fill = GridBagConstraints.NONE; 
     contentPane.add(button2, gbc); 

     JScrollPane scroller = new JScrollPane(contentPane); 

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

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new GridBagLayoutDemo().displayGUI(); 
      } 
     }); 
    } 
} 
+0

它適用於第一個領域。但是當我嘗試添加更多JTextField時,它們在整個高度上均勻分佈。有沒有辦法來防止這種情況呢? – Ankit

+0

@Ankit:太簡單了,只要評論這行'gbc.weighty = 1.0',雖然保持'weightx'的值如下:-),請回復,如果你想要別的東西,我不明白: - ) –

+0

使它從上面開始的第一部分是好的。但事情是,如果我使用'weighty',那麼它會從頂部開始,但是當我添加更多的字段時,它們不會從前一個字段的下面開始。相反,它始於中心。我想我正在使用錯誤的佈局。 – Ankit

2

嘗試與BorderLayout的controls.setLayout(new BorderLayout());,然後運用它爲您的JPanel controls.add(yourPanel, BorderLayout.PAGE_START);

我也有GridBagLayout的問題,所以我用BorderLayout解決了它,它工作這麼好。


所以我寫了你的小例子:

private void initComponents() { 

     controls = new Container(); 
     controls = getContentPane(); 
     controls.setLayout(new BorderLayout()); 

     panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 
     GridBagConstraints c = new GridBagConstraints(); 

     field = new JTextField(20); 
     c.gridx = 0; 
     c.gridy = 0; 
     panel.add(field, c); 

     one = new JButton("Go!"); 
     c.gridx = 1; 
     c.gridy = 0; 
     panel.add(one, c); 

     controls.add(panel, BorderLayout.PAGE_START); 

    } 

希望它能幫助!

2

對不起我的答案我是對的,你問什麼越位,但你爲什麼不使用,而不是網格包佈局的GroupLayout,那更容易處理

2

我認爲這可能是簡單的和可能,你可以

這個JPanel

  • JPanels包含JComponentGridLayout(約滾動通知,你必須改變滾動增量)

或使用最複雜的JComponents作爲

  • JPanels包含JComponent作爲項目的JList

  • put JPanels包含JComponent作爲行至JTable(僅與一個柱,帶或不帶TableHeader

0

添加在正確的一個面板和一個在底部。

右圖: 設置重量X1.0。 設置填充水平

底板: 設置重WeightY1.0。 設置填寫垂直

有可能是更好的辦法是,但對我來說這一個工作。

相關問題