2

I was using FlowLayout.CENTER to center a string and some checkboxes but it is not centering them. I've used this before and it worked fine. Here is the code:

import javax.swing.*;
import java.awt.*;

public class pizza extends JFrame {
//row 1
  JPanel row1 = new JPanel();
  JLabel select = new JLabel("Please select the size you would like"); 
//row 2
  JPanel row2 = new JPanel();
  JCheckBox ninein = new JCheckBox("9 inch, $5.00", false);
  JCheckBox twelvein = new JCheckBox("12 inch, $10.00", false);
  JCheckBox seventeenin = new JCheckBox("17 inch, $15.00", false);
//row 3
  JPanel row3 = new JPanel();
  JLabel toppingslab = new JLabel("Please select your toppings");
  JCheckBox cheese = new JCheckBox("Cheese, .50");
  JCheckBox pepperoni = new JCheckBox("Pepperoni, .50");
  JCheckBox onions = new JCheckBox("Onions, .50");
  JCheckBox peppers = new JCheckBox("Peppers, .50");
  JCheckBox bacon = new JCheckBox("Bacon! Free (because it's bacon)");
//row4
  JPanel row4 = new JPanel();
  JLabel totallab = new JLabel("Total");
  JTextField total = new JTextField(10);

public pizza(){
    setTitle("Pizza Ordering");
    setSize(500,500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
  total.setEnabled(false);

FlowLayout one = new FlowLayout(FlowLayout.CENTER);
  setLayout(one);

  row1.add(select);
  add(row1);

  row2.add(ninein);
  row2.add(twelvein);
  row2.add(seventeenin); 
  add(row2);

GridLayout two = new GridLayout(6,2);
  row3.setLayout(two);

  row3.add(toppingslab);
  row3.add(cheese);
  row3.add(pepperoni);
  row3.add(onions);
  row3.add(peppers);
  row3.add(bacon);
  add(row3);

FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
  setLayout(three);

  row4.add(totallab);
  row4.add(total);
  add(row4);  
    } 
public static void main(String[] args) {
    pizza pizz = new pizza();
}
}

Neither RIGHT or LEFT make a difference as well it simply aligns it to the right.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rostro
  • 148
  • 2
  • 3
  • 15
  • Some basic debugging techniques. 1) comment out your code and try adding the first panel to the frame and see if it works. 2) then add back in the code for the second panel. 3) then the third etc. etc. When it stops working then you can look at your code to see what is different. Also, follow standard Java naming conventions. Classes start with an upper case character. Show me one class in the API that doesn't follow this standard. Don't make up your own conventions especially if you want other people to read your code. – camickr Apr 07 '13 at 02:43

1 Answers1

4

The reason all components are right aligned is that you reset the layout of the JFrame to FlowLayout.RIGHT here:

FlowLayout three = new FlowLayout(FlowLayout.RIGHT);
setLayout(three);

This supercedes the earlier call to center align components.

Reimeus
  • 158,255
  • 15
  • 216
  • 276