多个 FlowLayouts 合二为一 class?

Multiple FlowLayouts in one class?

所以我有我的 Main,这是在里面完成的。

  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 class 我有:

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 中完成。我似乎无法正常工作。

编辑:

最终的样子:

所以我总共需要两个 FlowLayout。一个在顶部,一个在底部。它们都不包含中间的 TextPane。 这一切都在主要的 borderLayout 中出现。

提前致谢!

听起来很适合 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);

}

我认为 BorderLayout would work well. The big text field could be in the center and you could have the buttons and menus above and below it. Of course, A simple 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。