Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JLabel placement issues

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default JLabel placement issues

    I would like for my code below to display the word middle right below the word top. I can't figure out how to do this.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Label extends JFrame {
       private Container c;
       private JLabel n,w;
       public Label (String t) {
     
          super(t);
          setSize(400,200);
          c = getContentPane();
          c.setLayout(new BorderLayout());
          n = new JLabel("Top");
          n.setHorizontalAlignment(SwingConstants.LEFT);
          w = new JLabel("Middle");
          w.setHorizontalAlignment(SwingConstants.LEFT);
          c.add(n, BorderLayout.NORTH);
          c.add(w, BorderLayout.WEST);
          setVisible(true);
       }
       public static void main (String[] args) {
          Label bl = new Label("Layout");
          bl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: JLabel placement issues

    Like I said in your other post, you're really going to need to become very familiar with layouts. There's no way around it. Asking how to lay out each component every time you make a GUI is going to be pretty inefficient.

    Hint- You can stack layouts, meaning you can put a JPanel with one layout inside another JPanel with a different layout.

    Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    javapenguin (March 11th, 2011)

Similar Threads

  1. Variable declaration placement
    By 2nickpick in forum Java Theory & Questions
    Replies: 2
    Last Post: January 22nd, 2011, 11:34 AM
  2. A problem with JLabel!
    By niklas in forum AWT / Java Swing
    Replies: 5
    Last Post: December 19th, 2010, 05:51 AM
  3. A problem with JLabel!
    By niklas in forum Member Introductions
    Replies: 1
    Last Post: December 14th, 2010, 06:03 PM
  4. Placement of code within an application
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 12th, 2010, 08:26 PM
  5. how to output using JLabel?
    By qaromi in forum AWT / Java Swing
    Replies: 1
    Last Post: August 30th, 2009, 02:09 PM