import javax.swing.*; public class LabelExample { public static void main(String[] args) { JFrame frame = new JFrame("JLabel Vertical Alignment Example"); JLabel label = new JLabel("Hello, world!"); label.setVerticalAlignment(JLabel.TOP); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } }
import javax.swing.*; public class LabelExample { public static void main(String[] args) { JFrame frame = new JFrame("JLabel Vertical Alignment Example"); ImageIcon icon = new ImageIcon("image.png"); JLabel label = new JLabel(icon); label.setVerticalAlignment(JLabel.CENTER); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } }This code creates a JFrame window with a JLabel component that displays an image aligned to the center of the label. These examples use the javax.swing package library, which provides classes for creating graphical user interfaces (GUIs) in Java applications. Specifically, they use the JLabel class that is a part of this package, which provides a component for displaying text or images.