Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8344965

Problem of positioning JLabel correctly

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11

      A DESCRIPTION OF THE PROBLEM :
      No matter Where I set the values for panel.setbounds() or label.setlocation()
      For images , it puts them in the center of the frame for words , it puts them in the top middle of the frame.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1 go to eclipse 4.33.0
      2 mack a new product
      3 write the co in source code for Executable test
      3 save
      5 run as an application

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The picture moves based on values for the set bounds or Set location method
      ACTUAL -
      It depends.Are you putting in words in the label? Or are you putting in a file path for an image if it is the former it will basically Put the GUI in the top sensor of the window However for the latter it just puts it in the middle of the window

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;
      import java.awt.*;
      public class Main {
      public static void main(String[] args) {
      JFrame frame = new JFrame("Image set location");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel =new JPanel();
      JLabel label = new JLabel("ugg hello):<");
      panel.add(label);
      panel.setBounds(0, 0, 100, 50);
      label.setLocation(0, 0);
      frame.add(panel);
      frame.setSize(300 , 200);
      frame.setVisible(true);
      frame.repaint();
      }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: