import javax.swing.*; public class Example { public static void main(String[] args) { JFrame frame = new JFrame("Example"); JLabel label = new JLabel("Hello world!"); int height = label.getHeight(); System.out.println("Height of label is " + height); } }
import javax.swing.*; public class Example { public static void main(String[] args) { JFrame frame = new JFrame("Example"); JPanel panel = new JPanel(); JButton button = new JButton("Click me"); panel.add(button); int buttonHeight = button.getHeight(); panel.setSize(200, buttonHeight + 50); frame.add(panel); frame.pack(); frame.setVisible(true); } }In this example, we create a JFrame and a JPanel containing a JButton. We call the getHeight method on the JButton to determine its height, and use this value to set the height of the JPanel. We then add the JPanel to the JFrame and pack the JFrame to resize it to fit the components. This example also uses the javax.swing package library.