Home arrow Java & J2EE arrow Page 4 - Java's Basic User Interface Components

The Label Class - Java

To simplify user interaction and make data entry easier, you can use java controls. Controls are components, such as buttons, labels and text boxes, that can be added to containers like frames, panels and applets. The Java.awt package provides an integrated set of classes to manage user interface components.

  1. Java's Basic User Interface Components
  2. Adding a Control to a Container
  3. The Button Class
  4. The Label Class
  5. The Checkbox Class
  6. The CheckboxGroup Class
  7. The Choice List Class
  8. The TextField and TextArea Class
  9. Setting a Password Character
  10. TextArea
By: Gayathri Gokul
Rating: starstarstarstarstar / 22
October 20, 2003

print this article


Labels are created via the Label class. Labels are often used to identify the purpose of other components on a given interface; they cannot be edited directly by the user. Using a label is much easier than using a drawString( ) method because labels are drawn automatically and don’t have to handled explicitly in the paint( ) method. Labels can be laid out according to the layout manager, instead of using [x, y] coordinates, as in drawString( ).

To create a Label, use one of the following constructors:
  • Label( ) - creates a label with its string aligned to the left..
  • Label(String) - creates a label initialized with the given string, and aligned left.
  • Label(String, int) - creates a label with specified text and alignment indicated by the int argument: Label.Right, Label.Left and Label.Center.

Example 3

The following is a simple applet that creates a few labels in Helvetica bold.

<Applet code= “LabelTest.class”
Width = 500
Height = 100>

Import java.awt.*;

Public class LabelTest extends java.applet.Applet
Label left = new Label (“Left Wing”);
Label center = new Label (“Central Part”, Label.CENTER);
Label right = new Label (“Right Wing”, Label.RIGHT);
Font f1 = new Font(“Helvetica”, Font.Bold, 14);
GridLayout gd = new GridLayout(3,1);

Public void init(){

We can use label’s getText( ) method to indicate the current label’s text and setText( ) method to change the label’s text. We have also made use of setFont( ) method in the above example to change the label’s font and GridbagLayout to lay the components in the applet.

>>> More Java & J2EE Articles          >>> More By Gayathri Gokul

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: