aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help designing a GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help designing a GUI" Watch "Help designing a GUI" New topic
Author

Help designing a GUI

Jared Upton
Greenhorn

Joined: Apr 07, 2006
Posts: 15
I am making a gui but not sure how to place my text labels where i want them.I want it to something look like this but no idea how to move my labels. Like with the buttons i used a _ symbol in a label to space them apart.

Spoilable Regular
Total Weight
Total Length


Number of Trucks Needed
Semi
Refrig

Button Button




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


public class GUI {

public static void main (String[] args){
GUI process = new GUI();
process.makeGUI();


}

public void makeGUI() {

JButton process = new JButton("Process Order");
JButton deliver = new JButton("Deliver");


JLabel southspace = new JLabel("___________________________________________");
JLabel label1 = new JLabel("Regular");
JLabel label2 = new JLabel("Spoilable");
JLabel label3 = new JLabel("Total Weight");
JLabel label4 = new JLabel("test");
JLabel label5 = new JLabel("Total Length");
JLabel label6 = new JLabel("test");
JLabel label7 = new JLabel("Total Number of Trucks Needed");
JLabel label8 = new JLabel("Semis");
JLabel label9 = new JLabel("test");
JLabel label10 = new JLabel("Refrig");
JLabel label11 = new JLabel("test");


JFrame frame = new JFrame();
JPanel panelS = new JPanel();
JPanel panelC = new JPanel();
panelS.setBackground(Color.darkGray);
panelS.setLayout(new BoxLayout(panelS, BoxLayout.X_AXIS));
panelC.setLayout(new BoxLayout(panelC, BoxLayout.X_AXIS));

panelS.add(process);
panelS.add(southspace);
panelS.add(deliver);
panelC.add(label1);
panelC.add(label2);
panelC.add(label3);
panelC.add(label4);
frame.getContentPane().add(BorderLayout.SOUTH, panelS);
frame.getContentPane().add(BorderLayout.NORTH, panelC);
frame.setSize(500,500);
frame.setVisible(true);

}
}
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
You really need to play with combining panels with different layout managers to get the effect you want.

Don't think you can get anything decent looking without it, you may well need several layers of panels and layouts to get everything to look nice.

Experiment, remember what works and what doesn't, and start small.


42
Jared Upton
Greenhorn

Joined: Apr 07, 2006
Posts: 15
Okay this is going to be more difficult then I thought. I created it in a grid layout but how do I tell each label which cell in the Grid for it to be in?


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

public class GUI {

public static void main (String[] args){
GUI process = new GUI();
process.makeGUI();


}

public void makeGUI() {

JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(0,2));
panel1.setBackground(Color.darkGray);
panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));

panel1.add(new JButton("Process Order"));
panel1.add(new JLabel("___________________________________________"));
panel1.add(new JButton("Deliver"));
panel2.add(new JLabel("Regular",JLabel.CENTER));
panel2.add(new JLabel("Spoilable"));
panel2.add(new JLabel("Total Weight"));
panel2.add(new JLabel("test"));
panel2.add(new JLabel("Total Length"));
panel2.add(new JLabel("test"));
panel2.add(new JLabel("Total Number of Trucks Needed"));
panel2.add(new JLabel("Semis"));
panel2.add(new JLabel("test"));
panel2.add(new JLabel("Refrig"));
panel2.add(new JLabel("test"));
frame.getContentPane().add(BorderLayout.SOUTH, panel1);
frame.getContentPane().add(BorderLayout.NORTH, panel2);
frame.setSize(500,500);
frame.setVisible(true);





}
}
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
The only way to tell a GridLayout where a component should go is by the insertion order.
First component goes top left, last goes bottom righ.
Now I always have trouble remembering whether it fills up row by row or column by column, but you can see that easily enough (I prefer other layout managers, especially the Box).
Jared Upton
Greenhorn

Joined: Apr 07, 2006
Posts: 15
With box how do I add empty lines or space the stuff say one TABs length? I add spaces to the " this is My label" but it never spaces it over.
I had to add a label with "_____________" just to get the buttons spaced far enough apart.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Again, don't assume that a single panel with a single layout will give you everything you want.
Mix and match, combine things together.
You might want a grid with 2 boxes , a label, and a button for example.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help designing a GUI