Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a program for pizza orders with a GUI. My problem is that when I run the GUI, some of the elements won't show up. I'm pretty sure the problem is with the. 'BorderLayout.DIRECTION' bits. Only one item with each direction (north,south, etc) will show up. What i'm wondering is how I can fix that to make sure everything shows ups.

import java.awt.BorderLayout;
import java.awt.Container;

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


public class PizzaOrder {

	 private JComboBox createToppingsMenu() {
 		JComboBox comboBox = new JComboBox();
 		comboBox.addItem("Pepperoni");
 		comboBox.addItem("Sausage");
 		comboBox.addItem("Pinapple");
 		return comboBox;
 	}

	 private JComboBox createGetMenu() {
		JComboBox combo1Box = new JComboBox();
		combo1Box.addItem("Delivery");
		combo1Box.addItem("Pick Up");
		return combo1Box;
	}

	private JPanel createNamePanel() {
		JPanel namePanel = new JPanel();
		namePanel.add (new JLabel("Name:  "));
		namePanel.add (new JTextField(15));
		return namePanel;
	}

	private JPanel createAddressPanel() {
		JPanel addressPanel = new JPanel();
		addressPanel.add (new JLabel("Address:  "));
		addressPanel.add (new JTextField(15));
		return addressPanel;
	}

	private JPanel createPhonePanel() {
		JPanel phonePanel = new JPanel();
		phonePanel.add (new JLabel("Phone Number:  "));
		phonePanel.add (new JTextField(15));
		return phonePanel;
	}



	private JPanel createButtonPanel() {
		JPanel buttonPanel = new JPanel();
		buttonPanel.add (new JButton("Place order"));
		buttonPanel.add (new JButton("Cancel order"));
		return buttonPanel;
	}

	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.setTitle("Pizza Order");

		PizzaOrder order = new PizzaOrder();
		Container contentPane = f.getContentPane();

		JPanel namePanel = order.createNamePanel();
		contentPane.add(namePanel, BorderLayout.SOUTH);

		JPanel addressPanel = order.createAddressPanel();
		contentPane.add(addressPanel, BorderLayout.SOUTH);

		JPanel phonePanel = order.createPhonePanel();
		contentPane.add(phonePanel, BorderLayout.SOUTH);

		JComboBox comboBox = order.createToppingsMenu();
		contentPane.add(comboBox, BorderLayout.CENTER);

		JComboBox combo1Box = order.createGetMenu();
		contentPane.add(combo1Box, BorderLayout.CENTER);

		JPanel buttonPanel = order.createButtonPanel();
		contentPane.add(buttonPanel, BorderLayout.EAST);

		f.setSize(500, 500);

		f.setVisible(true);

	}

}


What I have tried:

looking it up but I haven't been able to figure anything out.
Posted
Updated 28-Apr-20 16:59pm
v2
Comments
Richard MacCutchan 29-Apr-20 4:24am    
I suspect that each panel that you add is overlaying any previous one with the same location (EAST, SOUTH etc.). Try creating one panel at each location you require, and just add the individual controls into each relevant panel. The panel manager should adjust their locations within the panel.
khaleesijamie 29-Apr-20 20:35pm    
How would I do that? Sorry i'm really new to java GUI.
Richard MacCutchan 30-Apr-20 4:49am    
Then you need to work through the tutorials to get a complete understanding of how to use these components. Here is a link to using the layout manager, but I recommend also looking at all the other sections: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900