• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Help designing a GUI

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);

}
}
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jared Upton
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic