1

So I am supposed to make a very simple fillout form like name: ______, and they are supposed to be underneath each other , but when I add these JTextFields and JLabels to the JFrame I get an empty JFrame.

JFrame frame = new JFrame("Name's Item Orders Calculator");

  JPanel panel = new JPanel();

  BorderLayout layout = new BorderLayout(0,2);

  JTextField nameField =  new JTextField(15);
  JTextField numberField = new JTextField(15);
  JTextField costField = new JTextField(15);
  JTextField amountField = new JTextField(15);

  JLabel name = new JLabel("Item Name: ");
  JLabel number = new JLabel("Number of: ");
  JLabel cost = new JLabel("Cost: ");
  JLabel amount = new JLabel("Amount owed: ");

  panel.add(name, BorderLayout.WEST);
  panel.add(number, BorderLayout.WEST);
  panel.add(cost, BorderLayout.WEST);
  panel.add(amount, BorderLayout.WEST);

  panel.add(nameField, BorderLayout.EAST);
  panel.add(numberField, BorderLayout.EAST);
  panel.add(costField, BorderLayout.EAST);
  panel.add(amountField, BorderLayout.EAST);

  /*
  panel.add(new JLabel("North"), BorderLayout.WEST);
  panel.add(new JLabel("Another North"), BorderLayout.WEST);
  */

  frame.pack();
  frame.add(panel);
  frame.setLayout(layout);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(400, 200);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
  • 1
    You should start by having a look at [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for ideas about how to use different layout managers. I'd recommend either `GridLayout` (for simplicity) or `GridBagLayout` for flexibility (but adds complexity) – MadProgrammer Jan 21 '18 at 23:15
  • Yep, what he just said. Looking at your code we can tell that you're just guessing how to do this, but why guess? Code from knowledge, and it's easily available. – Hovercraft Full Of Eels Jan 21 '18 at 23:17
  • Note that there are several "Next>" buttons on that link that MP just gave you above. – markspace Jan 21 '18 at 23:18
  • I will have to add buttons later anyways so it's perfect. Thanks for the help everyone! – Some Student Jan 21 '18 at 23:19

0 Answers0