import java.awt.*; import javax.swing.*; /** a first JFrame-plus-JPanel-plus-JLabel example Adapted from "Core Java" text, 11th edition @author Cay Horstmann @author adapted by Sharon Tuttle @version 2021-09-20 */ public class SimpleLabelTest { /** creates a simple frame @param args not used here */ public static void main(String[] args) { EventQueue.invokeLater( () -> { LabelFrame frame = new LabelFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } ); } } /** A simple frame that contains a panel with a JLabel */ class LabelFrame extends JFrame { // data fields private static final int DEFAULT_WIDTH = 300; private static final int DEFAULT_HEIGHT = 200; /** construct a simplest frame instance */ public LabelFrame() { setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); setTitle("A frame - with a Label!"); // add a LabelPanel to this frame -- and LabelPanel will // be a subclass of JPanel...! LabelPanel panel = new LabelPanel(); this.add(panel); } } /** a panel with some JLabel instances on it */ class LabelPanel extends JPanel { /** constructs a label panel instance */ public LabelPanel() { JLabel greeting = new JLabel("Hi! I am a JLabel!"); JLabel farewell = new JLabel("a farewell label!"); this.add(greeting); this.add(farewell); } }