Java Swing Tutorials - Herong's Tutorial Examples - v4.31, by Herong Yang
Creating Labels with java.awt.Label Class
This section provides a tutorial example on how to create a label with the java.awt.Label class.
Problem: I want to create a label with a text string.
Solution: This is easy, just instantiate an object of java.awt.Label, and add it to any container. Here is a sample program to show you how to do this:
/* LabelHello.java * Copyright (c) 1997-2018 HerongYang.com. All Rights Reserved. */ import java.awt.Frame; import java.awt.Label; public class LabelHello { public static void main(String[] a) { Frame f = new Frame("LabelHello"); Label l = new Label("Hello world!"); f.add(l); f.pack(); f.setVisible(true); } }
If you run this example, you will get:
Note: The pack() method causes the window to resize to fit the preferred size and layouts of its subcomponents.
Table of Contents
Introduction of Java Swing Package
Graphics Environment of the Local System
JCheckBox - Swing Check Box Class
JRadioButton - Swing Radio Button Class
JTextField - Swing Text Field Class
JComboBox - Swing Combo Box Class
Menu Bar, Menus, Menu Items and Listeners
Creating Internal Frames inside the Main Frame
Layout of Components in a Container
JEditorPane - The Editor Pane Class
SwingWorker - The Background Task Worker
►AWT (Abstract Windows Toolkit)
What Is AWT (Abstract Windows Toolkit)
HelloAWT.java - My First AWT Program
Closing AWT Frame and Terminating Application
Drawing Graphics - Using paint() on Frame or Component
►Creating Labels with java.awt.Label Class
Creating Buttons with java.awt.Button Class
AWT Button Action Handler at the Component Level
AWT Button Action Handler at the Frame Level
AWT Button Mouse Click Handler at the Frame Level
AWT TextField and ActionListener
MenuBarTest.java - AWT Menu Bar Test Program
MenuTest.java - AWT Menu Test Program
MenuItemTest.java - AWT Menu Item Test Program
MenuItemActionListenerTest.java - AWT Menu Item Action Listener