2011-12-13 51 views
3

我想寫我認爲應該是一個容易指定的對話框,我有幾個標籤和幾個文本區域對面,然後確定和取消按鈕。 在中間我有一個廣泛的成分(我使用例如標籤)爪哇擺動佈局不會做我想要的

|label| [combo box] 
|label| [txtfield] 
|label| [txtfield] 
| long label here | 


[btn1]  [btn2] 

我嘗試使用GridBagLayout的,它沒有做我想要什麼,我不知道我明白爲什麼。基本上我想把按鈕固定在對話框的底部。

以下鞋碼,我如何使用它:

package gui; 

import java.awt.Component; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextArea; 

public class GridBagLayoutExample2 extends JFrame { 

    private static final long serialVersionUID = -1972347726217162551L; 
    final private JLabel lbl1 = new JLabel("LABEL1: "); 
    final private JLabel lbl2 = new JLabel("LABEL2: "); 
    final private JLabel lbl3 = new JLabel("LABEL3: "); 
    final private JTextArea txt1 = new JTextArea(" "); 
    final private JComboBox cmb1 = new JComboBox(); 
    final private JTextArea txt2 = new JTextArea(""); 
    final private JLabel lblLine = new JLabel("a compenent on all the line"); 
    final private JButton btnOK = new JButton("OK"); 
    final private JButton btnCancel = new JButton("Cancel"); 

    public GridBagLayoutExample2() { 

     GridBagLayout bl = new GridBagLayout(); 
     Container pane = getContentPane(); 
     pane.setLayout(bl); 
     GridBagConstraints c = new GridBagConstraints(); 
     int r = 0; 

     placeComponentInGridBagLayout(lbl1, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(cmb1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(lbl2, pane, bl, c, 0, r, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(txt2, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(lbl3, pane, bl, c, 0, r, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(txt1, pane, bl, c, 1, r++, null, null, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(lblLine, pane, bl, c, 0, r++, null, null, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, 2, null, null, null); 
     placeComponentInGridBagLayout(btnOK, pane, bl, c, 0, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null); 
     placeComponentInGridBagLayout(btnCancel, pane, bl, c, 1, null, null, null, GridBagConstraints.PAGE_END, GridBagConstraints.HORIZONTAL, null, null, null, null); 

     setSize(new Dimension(850, 350)); 
     pack(); 
     setVisible(true); 
    } 

    public static void placeComponentInGridBagLayout(Component component, Container container, GridBagLayout bagLayout, GridBagConstraints c, Integer gridX, Integer gridY, Double weightX, Double weightY, Integer anchor, Integer fill, Integer ipadx, Integer ipady, 
      Integer gridWidth, Integer gridHeight) { 
     if (c == null) { 
      c = new GridBagConstraints(); 
     } 
     if (gridX != null) { 
      c.gridx = gridX; 
     } 
     if (gridY != null) { 
      c.gridy = gridY; 
     } 
     if (weightX != null) { 
      c.weightx = weightX; 
     } 
     if (weightY != null) { 
      c.weighty = weightY; 
     } 
     if (fill != null) { 
      c.fill = fill; 
     } 
     if (anchor != null) { 
      c.anchor = anchor; 
     } 
     if (ipadx != null) { 
      c.ipadx = ipadx; 
     } 
     if (ipady != null) { 
      c.ipady = ipady; 
     } 
     if (gridWidth != null) { 
      c.gridwidth = gridWidth; 
     } 
     if (gridHeight != null) { 
      c.gridheight = gridHeight; 
     } 
     bagLayout.setConstraints(component, c); 

     container.add(component); 
    } 

} 

任何想法,我做錯了嗎? 也有更現代的方式來實現Swing中的同一件事嗎?

感謝

+1

你可以顯示它*的功能嗎? –

+4

你永遠不會被迫只使用一個佈局管理器。例如使用BorderLayout作爲主佈局。然後使用FlowLayout爲按鈕創建一個面板,並將其添加到SOUTH。然後爲CENTER中的其他組件創建另一個面板。 – camickr

回答

2

當做一個簡單的表單時,SpringLayout是一個有用的佈局管理器。並且要將組件包裝在一條線上,使用BoxLayout會很有用。

下面是一個簡單的例子:

enter image description here

這裏是代碼:

public class LayoutDemo extends JFrame { 

    public LayoutDemo() { 

     JLabel label1 = new JLabel("label 1"); 
     JLabel label2 = new JLabel("label 2"); 
     JLabel label3 = new JLabel("label 3"); 
     JComboBox<String> combobox = new JComboBox<>(); 
     JTextField field1 = new JTextField(); 
     JTextField field2 = new JTextField(); 

     JPanel formPanel = new JPanel(new SpringLayout()); 
     formPanel.add(label1); 
     formPanel.add(combobox); 
     formPanel.add(label2); 
     formPanel.add(field1); 
     formPanel.add(label3); 
     formPanel.add(field2); 

     SpringUtilities.makeCompactGrid(formPanel, 3, 2, 2, 2, 3, 3); 

     JLabel longLabel = new JLabel("This is a longer label"); 
     longLabel.setAlignmentX(CENTER_ALIGNMENT); 

     JButton button1 = new JButton("Button 1"); 
     JButton button2 = new JButton("Button 2"); 

     JPanel buttonPanel = new JPanel(); 
     buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS)); 
     buttonPanel.add(button1); 
     buttonPanel.add(Box.createHorizontalGlue()); 
     buttonPanel.add(button2); 

     JPanel basePanel = new JPanel(); 
     basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.PAGE_AXIS)); 
     basePanel.add(formPanel); 
     basePanel.add(longLabel); 

     add(basePanel, BorderLayout.NORTH); 
     add(buttonPanel, BorderLayout.SOUTH); 
     pack(); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    public static void main(String[] args) { 
     new LayoutDemo(); 
    } 
} 

您可能需要使用BorderLayout到例如調整窗口大小時,不要拉伸formPanel。但我跳,我給你一些關於如何使用SpringLayout表單的想法。

2

的一個問題,我可以看到的是,你的長標籤在作爲OK按鈕相同網格單元去。 如果您想要將按鈕錨定在整個對話框的底部,請嘗試使用BorderLayout。爲按鈕製作一個單獨的面板並將其放入BorderLayout.SOUTH。然後把你的主要東西放在GridBagLayout的另一個面板中,然後把它放在BorderLayout.CENTER.這些按鈕不會與你的文本組件完全對齊,但我認爲沒關係。

2

可以使用的borderlayout和GridLayout的組合如下所示

enter image description here

package sof; 

import java.awt.BorderLayout; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class CustomLayoutFrame{ 
    public static void main(String args[]) { 
     JFrame frame = new JFrame("Custom Layout Frame"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new BorderLayout()); 
     JPanel centerPnl = new JPanel(); 
     centerPnl.setLayout(new BorderLayout()); 
     JPanel gridLayoutPane = new JPanel(); 
     GridLayout gridLayout = new GridLayout(3,2); 
     gridLayoutPane.setLayout(gridLayout); 
     gridLayoutPane.add(new JLabel("Label1")); 
     gridLayoutPane.add(new JComboBox()); 
     gridLayoutPane.add(new JLabel("Label2")); 
     gridLayoutPane.add(new JTextField()); 
     gridLayoutPane.add(new JLabel("Label3")); 
     gridLayoutPane.add(new JTextField()); 
     centerPnl.add(gridLayoutPane, BorderLayout.CENTER); 
     centerPnl.add(new JLabel("Long label should be going here"), BorderLayout.SOUTH); 

     frame.add(centerPnl, BorderLayout.CENTER); 

     JPanel bottomPanel = new JPanel(); 
     bottomPanel.add(new JButton("Button1")); 
     bottomPanel.add(new JButton("Button2")); 

     frame.add(bottomPanel, BorderLayout.SOUTH); 

     frame.setSize(300, 150); 
     frame.setVisible(true); 
     } 
} 
+0

我想要一個GridBagLayout的原因是能夠使屏幕最大化,並且組件仍然合理地佈置好,我試過了你的代碼,它對你設置的尺寸很好,但是我的尺寸是(850,585)。 。我希望前三個標籤和控件位於頂部,大標籤可以伸展以填充寬度(與代碼相同)和按鈕保持錨定在底部。 – Charbel

+0

@Charbel:聽起來像我的答案中的佈局。或者你不喜歡它什麼?如果它不夠好,給我們更多的細節是很好的。 – Jonas

+0

@Jonas確實被標記爲答案;-)謝謝! – Charbel

2

一個很好LayoutManager爲這些目的是從JGoodies數據的FormLayout