2

so I have my Main, and this is done inside of it.

  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);

In my CarGUI class I have:

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);
}

So basically what has to happen here, is I need to make another flow layout, called 'SouthLayout', and in the main, I need to put it to that one. However, the flowlayout has to be done inside CarGUI. I can't seem to get this working.

EDIT:

What it has to look like eventually:

enter image description here

So I'll be needing two FlowLayouts in total. One for the top, and one at the bottom. Neither of them include the TextPane in the middle. This all comes in a borderLayout in the main.

Thanks in advance!

Paramone
  • 2,634
  • 4
  • 31
  • 58
  • 1
    See [MigLayout](http://miglayout.com/). It's far better than the default ones. – elias Oct 13 '15 at 17:05
  • So what is the problem you're having? You have shown what it should look like, can you also show what it looks like right now? – Shahzad Oct 13 '15 at 17:13

2 Answers2

2

Sounds like a good candidate for BorderLayout

I've modified your code to get a good start at implementing it, given what you have shown us:

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);

}
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
0

I think a 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 would also do the job fine. Here is a simple example on how to achieve this with a 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();
    }
}

Each individual 'JPanel', or any other container, can have its own layout manager. So if you want a different layout in part of your application, add a JPanel with the layout you need.

Fjotten
  • 347
  • 3
  • 13