0

I used following code to display multiple lables on java GUI app. But when i used the location method for the last label object, it was not worked properly. The case is always effected to the last object of the flow. follwoing is the code segment and the screen shot of the output window. please give me your feedback to solve this. Thanks!

package gui.creating;
/**
 *
 * @author Dilan Dinushka
 */
import javax.swing.*;
public class GUICreating 
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
        JFrame frame1 = new JFrame();
        frame1.setSize(500,500);
        frame1.setTitle("BASIC GUI APPLICATION");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);

        JLabel lbl1 = new JLabel("Welcome to IDM!");
        lbl1.setBounds(100,100,200,50);
        frame1.add(lbl1);


       JLabel lbl3 = new JLabel("Thank You");
        lbl3.setBounds(100,200,200,50);
        frame1.add(lbl3);

        JLabel lbl2 = new JLabel("Nuturing Achievers");
        lbl2.setBounds(100,150,200,50);
        frame1.add(lbl2);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 31 '15 at 09:46
  • *"follwoing is the code segment and the screen shot of the output window"* Where is the screenshot? Link to it.. As far as I can tell from that code, only one label would be displayed. – Andrew Thompson May 31 '15 at 09:49
  • Possible duplicate of [Why does the first panel added to a frame disappear?](http://stackoverflow.com/q/30361149/418556) – Andrew Thompson May 31 '15 at 09:49
  • `frame1.setVisible(true);` should be the very last thing done to a top level container, directly after `pack()` to make the TLC the smallest size needed to display the components. – Andrew Thompson May 31 '15 at 09:53

2 Answers2

0

Location method is deprecated I think. Look in javadocs and check out this post, it may help you Java Swing - JLabel Location

Community
  • 1
  • 1
somi
  • 115
  • 8
0

When execute this code, all labels show properly based on parameters that you define on setBounds function.

When use setLocation

lbl2.setLocation(20, 20);

method on last label object, it will display label according to x-axis & y-axis on your frame. It will works accordingly parameters that defines in setLocation method.

lbl2.location - This method is deprecated. So it may cause error or not display label properly.

If you use any other location method then explain which one you are using.

Puneet Chawla
  • 5,729
  • 4
  • 16
  • 33