Java JComponent get x/y/height/width

Description

Java JComponent get x/y/height/width

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main extends JFrame {

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent comp = new JLabel("Test label");
    /*  w w w .j a v a2 s .co  m*/
    
    getContentPane().add(comp);
    pack();

    System.out.println("X:" + comp.getX() + " Y:" + comp.getY() + " width:"
        + comp.getWidth() + " height:" + comp.getHeight());
    
    setVisible(true);
  }

  public static void main(String arg[]) {
    new Main();
  }
}



PreviousNext

Related