Java
  Home arrow Java arrow Page 4 - Java's Basic User Interface Components
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java's Basic User Interface Components
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 13
    2003-10-20

    Table of Contents:
  • Java's Basic User Interface Components
  • Adding a Control to a Container
  • The Button Class
  • The Label Class
  • The Checkbox Class
  • The CheckboxGroup Class
  • The Choice List Class
  • The TextField and TextArea Class
  • Setting a Password Character
  • TextArea

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java's Basic User Interface Components - The Label Class


    (Page 4 of 10 )

    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>
    </applet>
    */

    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(){
    setFont(f1);
    setLayout(gd);
    add(left);
    add(center);
    add(right);
    }
    }


    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 Articles
    More By Gayathri Gokul


     

       

    JAVA ARTICLES

    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway