JLabel label1 = new JLabel("Hello World!"); int height = label1.getHeight(); System.out.println("Height of label1: " + height);
JLabel label2 = new JLabel(new ImageIcon("image.jpg")); frame.add(label2); frame.pack(); int height = label2.getHeight(); System.out.println("Height of label2: " + height);This code creates a new JLabel instance with an image loaded from a file, adds it to a JFrame, packs the frame to fit the size of the label, gets the height of the label using the getHeight() method, and prints it to the console. Both examples use the javax.swing package library to create and manipulate JLabel components.