GUI Layout

This is my code:

import javax.swing.*;

import java.awt.*;

public class InputWindow extends JFrame

{

private JPanel panel = new JPanel();

private JButton inputButton = new JButton("Input");

private JLabel rainfallLabel = new JLabel("Rainfall: ");

private JLabel rainfallUnitLabel = new JLabel("mm");

private JLabel wheatLabel = new JLabel("Wheat Price: $");

private JLabel barleyLabel = new JLabel("Barley Price: $");

private JLabel sorghumLabel = new JLabel("Sorghum Price: $");

private JTextField enterRainfall = new JTextField(3);

private JTextField enterWheatPrice = new JTextField(3);

private JTextField enterBarleyPrice = new JTextField(3);

private JTextField enterSorghumPrice = new JTextField(3);

private final int WINDOW_WIDTH = 500;

private final int WINDOW_HEIGHT = 375;

public InputWindow()

{

super("DOTA Farming");

setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

buildPanel();

setVisible(true);

}

private void buildPanel()

{

add(panel);

addComponents(rainfallLabel, enterRainfall);

panel.add(rainfallUnitLabel);

addComponents(wheatLabel, enterWheatPrice);

addComponents(barleyLabel, enterBarleyPrice);

addComponents(sorghumLabel, enterSorghumPrice);

panel.add(inputButton);

}

private void addComponents(JLabel label, JTextField textField)

{

panel.add(label);

panel.add(textField);

}

}

How do I set the layout so that it appears this way:

label textField label

label textField

label textField

label textField

button

[1701 byte] By [hornetsfan16a] at [2007-11-27 5:27:20]
# 1
also does anyone have any suggestions for making declarations because it looks a bit too cluttered
hornetsfan16a at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...
# 2
To get the layout you want, usually takes several nested panels each may have a different LayouManager. Try drawing it out on a piece of paper first.
floundera at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...
# 3
i thought about using panels but then that would make my declarations even longer and more cluttered
hornetsfan16a at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...
# 4
I guess you will just have to put up with how it is at the moment then. Or use the super secret MagicPixieDust class.If you are worried about it being cluttered then use more classes or the very least more methods.
floundera at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...
# 5
do you have a suggestion on how I can make the declarations through more methods
hornetsfan16a at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...
# 6

You probably have no choice but to declare the components as instance variables but you can move all the instantiations to methods. You currently have a buildPanel method. So you could have more, one method for each panel. But seriously why are you worrying about your code looking cluttered. What you should be focussing on is getting the GUI layout correct and everything working.

floundera at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...
# 7
ah ok thanks I understand what I can do. It's because i'm doing an assignment and we get marks for "style" of code. I've lost marks before when the declarations were too long at the beginning
hornetsfan16a at 2007-7-12 14:48:49 > top of Java-index,Java Essentials,New To Java...