• We just launched and are currently in beta. Join us as we build and grow the community.

No Layout in Java (Absolute Positioning)

WherezDaWarez

One-Liner Genius
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
59
Likes
100
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial will teach you how to provide a java swing program without a layout. We will use the keyword null here in the setLayout method and we will do the Absolute Positioning using the setBounds method.

So, now let's start this tutorial!


1. Open JCreator or NetBeans and make a java program with a file name of noLayout.java.

2. Import javax.swing.* package library import javax.swing.*; //used to access JFrame, JLabel, and JTextField class.

  1. import

    javax.swing.*

    ;

    //used to access JFrame, JLabel, and JTextField class

3. Now, we will have a User Interface of having the LookAndFeelDecorated UI and set it to true.

  1. JFrame

    .setDefaultLookAndFeelDecorated

    (

    true

    )

    ;

We will initialize variables in our Main, variable frame as JFrame, label1 and label2 as JLabel, and textField1 and textField2 as JTextField.

  1. JLabel

    label1 =

    new

    JLabel

    (

    "Name:"

    )

    ;
  2. JLabel

    label2 =

    new

    JLabel

    (

    "Website:"

    )

    ;

  3. JTextField

    textField1 =

    new

    JTextField

    (

    "Lyndon R. Bermoy"

    , 15

    )

    ;
  4. JTextField

    textField2 =

    new

    JTextField

    (

    "www.sourcecodester.com"

    , 15

    )

    ;

4. To set the layout without a layout, we will use the keyword null here in the setLayout method of the frame.
  1. frame.getContentPane

    (

    )

    .setLayout

    (

    null

    )

    ;

Now, we will have an absolute positioning of our components and we will use the setBounds method. The setBounds method specify the x-position, y-position, width of the components, and height of the components respectively. Here, we will use the setBounds method of the two labels and two textfields and make sure that they don't overlap with each other.

  1. label1.setBounds

    (

    20

    , 20

    , 200

    , 40

    )

    ;
  2. label2.setBounds

    (

    20

    , 60

    , 200

    , 40

    )

    ;

  3. textField1.setBounds

    (

    125

    , 20

    , 200

    , 40

    )

    ;
  4. textField2.setBounds

    (

    125

    , 65

    , 200

    , 40

    )

    ;

5. Add then the components using the add method.
  1. frame.getContentPane

    (

    )

    .add

    (

    label1)

    ;
  2. frame.getContentPane

    (

    )

    .add

    (

    textField1)

    ;

  3. frame.getContentPane

    (

    )

    .add

    (

    label2)

    ;
  4. frame.getContentPane

    (

    )

    .add

    (

    textField2)

    ;

Lastly, set its size, visibility to true, and have its close operation.

  1. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  2. frame.setSize

    (

    400

    , 200

    )

    ;
  3. frame.setVisible

    (

    true

    )

    ;


Output:

nolayout.png


Here's the full code of this tutorial:

  1. import

    javax.swing.*

    ;

    //used to access JFrame, JLabel, and JTextField class


  2. public

    class

    noLayout{

  3. public

    static

    void

    main(

    String

    [

    ]

    args)

    {

  4. JFrame

    .setDefaultLookAndFeelDecorated

    (

    true

    )

    ;
  5. JFrame

    frame =

    new

    JFrame

    (

    "Without Layout"

    )

    ;

  6. JLabel

    label1 =

    new

    JLabel

    (

    "Name:"

    )

    ;
  7. JLabel

    label2 =

    new

    JLabel

    (

    "Website:"

    )

    ;

  8. JTextField

    textField1 =

    new

    JTextField

    (

    "Lyndon R. Bermoy"

    , 15

    )

    ;
  9. JTextField

    textField2 =

    new

    JTextField

    (

    "www.sourcecodester.com"

    , 15

    )

    ;

  10. label1.setBounds

    (

    20

    , 20

    , 200

    , 40

    )

    ;
  11. label2.setBounds

    (

    20

    , 60

    , 200

    , 40

    )

    ;

  12. textField1.setBounds

    (

    125

    , 20

    , 200

    , 40

    )

    ;
  13. textField2.setBounds

    (

    125

    , 65

    , 200

    , 40

    )

    ;


  14. frame.getContentPane

    (

    )

    .setLayout

    (

    null

    )

    ;

  15. frame.getContentPane

    (

    )

    .add

    (

    label1)

    ;
  16. frame.getContentPane

    (

    )

    .add

    (

    textField1)

    ;

  17. frame.getContentPane

    (

    )

    .add

    (

    label2)

    ;
  18. frame.getContentPane

    (

    )

    .add

    (

    textField2)

    ;

  19. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  20. frame.setSize

    (

    400

    , 200

    )

    ;
  21. frame.setVisible

    (

    true

    )

    ;
  22. }
  23. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top