billabrian6
billabrian6

Reputation: 431

I'm having trouble with Java Layout

This is my first GUI application and I'm having trouble making it look neat. I have tried several layouts and tinkered with them e.g flow, grid, border. When I run the program everything is just jumbled together.

I would like it to look like:

Unloaded Measurement      |textfield| |textfield|
Loaded Rider Measurement  |textField| |textfield|
Loaded Bike Measurement   |textField| |Textfield|

                   |Button|
_______________________________________________________________________________________

Race Sag: |TextField|
Free Sag: |TextField|
Race Sag Notes: |       TextField         |
Free Sag Notes: |       TextField         |

Here is a screenshot to help understand what my issue is: Screenshot

The top is for user input and the bottom is calculated output. I hope that I have given enough details for some help with this. I really appreciate anyone that helps out! Here is my code:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel messageLabel;
private JLabel messageLabel1;
private JLabel messageLabel2;
private JLabel raceSagLabel;
private JLabel freeSagLabel;
private JTextField wholeTextField;
private JTextField wholeTextField1;
private JTextField wholeTextField2;
private JTextField fracTextField;
private JTextField fracTextField1;
private JTextField fracTextField2;
private JTextField raceSagText;
private JTextField freeSagText;
private JTextField noteText;
private JTextField noteText1;
private JButton calcButton;
private final int WINDOW_WIDTH = 575;
private final int WINDOW_HEIGHT = 400;


/*===============================================================================
    Project : test.java - SagCalculator
    Author  : Brian Green
    Date    : Jan 10, 2013
    Abstract:   

  ===============================================================================*/
public static void main(String[] args) {
    @SuppressWarnings("unused")
    Main sc = new Main();
}
public Main(){
    setTitle("Rider Sag Calculator");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setLayout();
    buildPanel();
    add(panel);
    setVisible(true);
}
private void buildPanel(){
    messageLabel = new JLabel("Enter Unloaded Stand Measurement");
    messageLabel1 = new JLabel("Enter Loaded with Rider Measurement");
    messageLabel2 = new JLabel("Enter Loaed without Rider Measurement");
    wholeTextField = new JTextField(3);
    fracTextField = new JTextField(3);
    wholeTextField1 = new JTextField(3);
    fracTextField1 = new JTextField(3);
    wholeTextField2 = new JTextField(3);
    fracTextField2 = new JTextField(3);
    calcButton = new JButton("Calculate");
    raceSagLabel = new JLabel("Race Sag: ");
    raceSagText = new JTextField(5);
    freeSagLabel = new JLabel("Free Sag: ");
    freeSagText = new JTextField(5);
    noteText = new JTextField(30);
    noteText1 = new JTextField(30);

    calcButton.addActionListener(new CalcButtonListener());
    panel = new JPanel();

    panel.add(messageLabel);
    panel.add(wholeTextField);
    panel.add(fracTextField);
    panel.add(messageLabel1);
    panel.add(wholeTextField1);
    panel.add(fracTextField1);
    panel.add(messageLabel2);
    panel.add(wholeTextField2);
    panel.add(fracTextField2);
    panel.add(calcButton);  
    panel.add(raceSagLabel);
    panel.add(raceSagText);
    panel.add(freeSagLabel);
    panel.add(freeSagText);
    panel.add(noteText);
    panel.add(noteText1);



}

If for some reason you need to see more of the code, just let me know. I will be happy to provide it. Thanks for the help!

I got this all worked out! I would like to say thanks to @trashgod for his suggestion: enter image description here

Upvotes: 1

Views: 150

Answers (3)

trashgod
trashgod

Reputation: 205875

GroupLayout, see here, does a nice job on labeled forms, and it is manageable as a subpanel.

image

Upvotes: 1

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

It's possible to achieve the layout that you want with some of the other suggestions here (including GridBagLayout), but the easiest solution by far is to use the JGoodies FormLayout, since it's explicitly designed for creating layouts where you need to line up fields and labels.

Upvotes: 2

kutschkem
kutschkem

Reputation: 8163

Try calling pack() on your JFrame, the layout doesn't get applied immediately when you set it. Alternatively, you can use validate(), which should lay out the component as well. Btw. in your sample code you don't set a layout manager.

Upvotes: 2

Related Questions