简体   繁体   中英

How to fix location error on java labels in GUI Application Used SWING?

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);
    }
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM