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 4 of 4

Thread: Layout related problem

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Layout related problem

    hi please help me with this code, i am new to Java and i have made this code using border layout but when i maximize my screen all components change there positions , how can i keep them in any desired position ?

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

    public class MyFrame
    {

    JLabel lbsex;
    JTextField tf;
    JButton bname;
    JRadioButton rbmale;
    JRadioButton rbfemale;
    JLabel lbdob;
    JLabel lbadd;
    JTextArea tarea;

    MyFrame()
    {
    JFrame frm=new JFrame();
    frm.getContentPane().setLayout(new BorderLayout());
    frm.setSize(300, 240);




    bname=new JButton("Name");
    Dimension dim=new Dimension(100, 25);
    bname.setMinimumSize(dim);

    tf=new JTextField(10);
    Dimension dim1=new Dimension(400, 25);

    tf.setMinimumSize(dim1);
    tf.setMaximumSize(dim1);

    Box b1=Box.createHorizontalBox();
    Box b2=Box.createHorizontalBox();

    b1.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    b2.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    b1.add(bname);
    b1.add(Box.createRigidArea(new Dimension(0, 4)));
    b1.createHorizontalGlue();

    Dimension minSize = new Dimension(5, 100);
    Dimension prefSize = new Dimension(5, 100);
    Dimension maxSize = new Dimension(Short.MAX_VALUE, 100);
    b1.add(new Box.Filler(minSize, prefSize, maxSize));


    b1.add(tf);


    lbsex=new JLabel("Sex");


    rbmale=new JRadioButton("Male");


    rbfemale=new JRadioButton("Female");

    b2.add(lbsex);

    b2.add(rbmale);
    b2.add(Box.createRigidArea(new Dimension(0, 4)));
    b2.add(rbfemale);


    lbdob=new JLabel("Date Of Birth");


    frm.getContentPane().add(b1, BorderLayout.NORTH);
    frm.getContentPane().add(b2, BorderLayout.CENTER);

    frm.pack();
    frm.setVisible(true);

    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;

    }
    public static void main (String args[])
    {
    //MyFrame mfrm=new MyFrame();

    SwingUtilities.invokeLater(new Runnable(){ public void run(){ new MyFrame();}});

    }
    }


  2. #2
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Layout related problem

    Hello Gaurev18,

    Check out these Oracle tutorials about Creating a Custom Layout Manager and Absolute Positioning. Those should help you get the desired result.

    Best of luck.

  3. #3
    Member
    Join Date
    Sep 2011
    Location
    United States
    Posts
    30
    My Mood
    Fine
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Layout related problem

    Hello Gaurev18,

    Check out these Oracle tutorials about Creating a Custom Layout Manager and Absolute Positioning. Those should help you get the desired result.

    Best of luck.

    EDIT: Not sure how I got a double post :/

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout related problem

    Thanks SerratedMind, :-)

Similar Threads

  1. problem related combobox nd database
    By harsimran in forum Java Theory & Questions
    Replies: 1
    Last Post: September 9th, 2011, 02:27 PM
  2. Problem with layout manager
    By mDennis10 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 4th, 2011, 07:47 PM
  3. Calculator layout problem
    By Choiseymitsu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2011, 08:04 AM
  4. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  5. problem in managing layout
    By namreen in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2010, 12:52 AM