2015-10-13 16 views
2

所以我有我的主,這是在它內部完成的。多個FlowLayouts在一個類中?

JFrame CF = new JFrame(); 
    CF.setLayout(new BorderLayout()); 
    CF.add(new CarGUI(), BorderLayout.NORTH); 
    // CF.add(new CarGUI(), BorderLayout.SOUTH); 
    //' South FlowLayout ' here^ 
    CF.setSize(600,400); 
    CF.setVisible(true); 

在我CarGUI I類有:

public class CarGUI extends JPanel { 

private CarTaxManager manager; 
private JLabel lpLabel; 
private JTextField searchField; 
private JButton searchButton; 

public CarGUI(){ 
    FlowLayout NorthLayout = new FlowLayout(); 
    //this.setLayout(new FlowLayout()); 
    this.setLayout(NorthLayout); 
    lpLabel = new JLabel("License Plate"); 
    searchField = new JTextField(10); 
    searchButton = new JButton("Search"); 

    add(lpLabel); 
    add(searchField); 
    add(searchButton); 
} 

所以基本上必須在這裏發生,是我需要另一個流佈局,被稱爲「SouthLayout」,並在主,我需要把它放在那一個。但是,流程佈局必須在CarGUI內完成。我似乎無法得到這個工作。

編輯:

什麼它看起來像最終:

enter image description here

所以我會需要總共兩個FlowLayouts。一個用於頂部,另一個用於底部。它們都不包括中間的TextPane。 這一切都在主要的borderLayout中。

在此先感謝!

+1

請參閱[MigLayout](http://miglayout.com/)。這比默認的好多了。 – elias

+0

那麼你遇到了什麼問題?你已經展示了它應該是什麼樣子,你還可以展示它現在的樣子嗎? –

回答

2

聽起來像是給一個很好的候選人BorderLayout

我修改代碼以實現它得到一個良好的開端,你已經告訴我們:

public class CarGUI extends JPanel { 

private CarTaxManager manager; 
private JLabel lpLabel; 
private JTextField searchField; 
private JButton searchButton; 

public CarGUI(){ 
    setLayout(new BorderLayout()); 

    JPanel north = new JPanel(); 
    north .setLayout(new FlowLayout()); 
    lpLabel = new JLabel("License Plate"); 
    searchField = new JTextField(10); 
    searchButton = new JButton("Search"); 
    north.add(lpLabel); 
    north.add(searchField); 
    north.add(searchButton); 
    add(north, BorderLayout.NORTH); 


    JPanel center = new JPanel(); 
    center.setLayout(new FlowLayout()); 
    //TODO add components to center 
    add(center, BorderLayout.CENTER); 

    JPanel south= new JPanel(); 
    south.setLayout(new FlowLayout()); 
    //TODO add components to south 
    add(south, BorderLayout.SOUTH); 

} 
+0

請注意,您不需要將新JPanel的佈局設置爲新的FlowLayout - 這是默認設置。 – Nulano

+0

我更喜歡明確 –

0

我想BorderLayout會工作好。大文本字段可能位於中心,您可以在上方和下方有按鈕和菜單。當然,簡單的BoxLayout也可以很好地完成這項工作。這裏有一個關於如何用BorderLayout實現這個功能的簡單例子。

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.FlowLayout; 
import javax.swing.BorderFactory; 
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 CarGUI extends JFrame { 

    public CarGUI() { 
     initGUI(); 
    } 

    public void initGUI() { 
     setLayout(new BorderLayout()); 
     setSize(600, 400); 
     initNorthGUI(); 
     initCenterGUI(); 
     initSouthGUI(); 
     setVisible(true); 
    } 

    private void initNorthGUI() { 
     JPanel northPanel = new JPanel(); 
     northPanel.setLayout(new FlowLayout()); 
     northPanel.add(new JLabel("License Plate")); 
     northPanel.add(new JTextField(10)); 
     northPanel.add(new JButton("Search")); 
     add(northPanel, BorderLayout.PAGE_START); 

    } 

    private void initCenterGUI() { 
     JLabel centerPanel = new JLabel("Center"); 
     centerPanel.setBorder(BorderFactory.createLineBorder(Color.black)); 
     add(centerPanel, BorderLayout.CENTER); 
    } 

    private void initSouthGUI() { 
     JPanel southPanel = new JPanel(); 
     southPanel.setLayout(new FlowLayout()); 
     southPanel.add(new JButton("Some Button")); 
     southPanel.add(new JComboBox()); 
     add(southPanel, BorderLayout.PAGE_END); 
    } 

    public static void main(String args[]) { 
     CarGUI c = new CarGUI(); 
    } 
} 

每一個人的JPanel的',或其他任何container,可以有自己的佈局管理器。所以如果你想在你的應用程序的一部分中使用不同的佈局,請添加一個帶有你需要的佈局的JPanel。