user7427726
user7427726

Reputation: 19

Small trouble with BorderLayout

I'm trying to set up the frame for a pizza order form, and I'm having trouble with the orderPanel and the buttonsPanel. I can either make one or the other show up, but not both? In this current code I'm posting, the buttons are showing, but the textbox/orderPanel isn't. And I've gotten it so the orderPanel DOES show, but then it hides the buttons, which is also not good. I want the buttons at the very bottom and the orderPanel right above it; how can I do that?

class PizzaOrderFrame extends JFrame 
{
final private JPanel crustPanel, sizePanel, toppingsPanel, orderPanel, buttonsPanel;
final private TitledBorder crustBorder, sizeBorder, toppingsBorder, orderBorder;
final private JButton quitButton, clearButton, orderButton;
final private JTextArea orderTextArea;
final private JRadioButton thin, regular, deepDish;
final private JCheckBox pepperoni, sausage, bacon, extraCheese, olives, mushrooms;
double smallSizeCost = 8.0;
double mediumSizeCost = 12.0;
double largeSizeCost = 16.0;
double superSizeCost = 20.0;
double toppingsCost = 1.0;
double toppingsCount = 0;
double tax = 0.07;
double orderSubTotal = 0;
double orderTotal = 0;

public PizzaOrderFrame() 
{
    setTitle("Pizza Order Form");
    Toolkit kit = Toolkit.getDefaultToolkit();
    Dimension screenSize = kit.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;
    double setScreen = screenWidth * .80;
    double setScreen3 = screenHeight * .80;
    int setScreen2 = (int) setScreen;
    int setScreen4 = (int) setScreen3;
    setSize(setScreen2, setScreen4);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    crustPanel = new JPanel();
    crustBorder = new TitledBorder("Select your crust");
    crustBorder.setTitleJustification(TitledBorder.CENTER);
    crustBorder.setTitlePosition(TitledBorder.TOP);
    crustPanel.setBorder(crustBorder);
    thin = new JRadioButton("Thin");
    regular = new JRadioButton("Regular");
    deepDish = new JRadioButton("Deep-Dish");
    ButtonGroup group = new ButtonGroup();
    group.add(thin);
    group.add(regular);
    group.add(deepDish);
    crustPanel.add(thin);
    crustPanel.add(regular);
    crustPanel.add(deepDish);
    add(crustPanel, BorderLayout.WEST);

    sizePanel = new JPanel();
    sizeBorder = new TitledBorder("Select your size");
    sizeBorder.setTitleJustification(TitledBorder.CENTER);
    sizeBorder.setTitlePosition(TitledBorder.TOP);
    sizePanel.setBorder(sizeBorder);
    String[] sizeOptions = new String [] {"Small", "Medium", "Large", "Super" };
    JComboBox<String> size = new JComboBox<>(sizeOptions);
    String selectedSize = (String) size.getSelectedItem();
    sizePanel.add(size);
    add(sizePanel, BorderLayout.CENTER);

    toppingsPanel = new JPanel();
    toppingsBorder = new TitledBorder("Select your toppings");
    toppingsBorder.setTitleJustification(TitledBorder.CENTER);
    toppingsBorder.setTitlePosition(TitledBorder.TOP);
    toppingsPanel.setBorder(toppingsBorder);
    pepperoni = new JCheckBox("Pepperoni");
    sausage = new JCheckBox("Sausage");
    extraCheese = new JCheckBox("Extra Cheese");
    mushrooms = new JCheckBox("Mushrooms");
    olives = new JCheckBox("Olives");
    bacon = new JCheckBox("Bacon");
    toppingsPanel.add(pepperoni);
    toppingsPanel.add(sausage);
    toppingsPanel.add(extraCheese);
    toppingsPanel.add(mushrooms);
    toppingsPanel.add(olives);
    toppingsPanel.add(bacon);
    add(toppingsPanel, BorderLayout.EAST);

    orderPanel = new JPanel();
    orderBorder = new TitledBorder("Your Order");
    orderBorder.setTitleJustification(TitledBorder.CENTER);
    orderBorder.setTitlePosition(TitledBorder.TOP);
    orderPanel.setBorder(orderBorder);
    orderTextArea = new JTextArea();
    JScrollPane orderSP = new JScrollPane(orderTextArea);
    orderSP.setPreferredSize( new Dimension( 300, 100 ) );
    orderPanel.add(orderSP);
    add(orderPanel, BorderLayout.SOUTH);

    buttonsPanel = new JPanel();
    quitButton = new JButton("Quit");
    clearButton = new JButton("Clear");
    orderButton = new JButton("Order");
    buttonsPanel.add(quitButton);
    buttonsPanel.add(clearButton);
    buttonsPanel.add(orderButton);
    add(buttonsPanel, BorderLayout.PAGE_END);

}
}

Upvotes: 0

Views: 27

Answers (1)

Nate
Nate

Reputation: 16898

According to the JavaDoc for PAGE_END

For Western, left-to-right and top-to-bottom orientations, this is equivalent to SOUTH.

BorderLayout "areas" can only contain one component, so if you call add() multiple times using the same "area", only the last one is shown.

One way to get the layout you want is by creating another Panel, give it a BorderLayout, add orderPanel to the new Panel's NORTH, buttonsPanel to the new Panel's SOUTH, and then add the new Panel to the existing pizzaOrderFrame's SOUTH.

Upvotes: 1

Related Questions