Adv Java - [Swing: ImageIcon/JLabel Classes]

♠ Posted by Unknown in at 00:26

ImageIcon Class


In Swing, icons are encapsulated by the ImageIcon class, which paints an icon from an image. Two of lists constructors are show here:

     ImageIcon(String filename)
     ImageIcon(URL url)

The first form used the image in the file named “filename”. The second form used the image in the resource identified by “url”.


The ImageIcon class implements the Icon interface that declares the methods show here:

     Method                                               Description
     int getIconHeight()                              Returns the height of the icon in pixels.
     Int getIconWidth()                               Returns the width of the icon in pixels.
     Void painIcon()                                   Paints the icon at position x, y on the
                                                              graphics context g. Additional
                                                              information about the paint operation
                                                              can be provided in comp.

JLabel Class

Swing labels are instances of the JLabel class, which extends JComponent. It can display text and/or an icon. Some of its constructors are show here:

     JLabel(Icon i)
     Label(String s)
     JLabel(String s, Icon I, int align)

Image: Java JLabel ExampleHere, s and i are the text and icon used for the label. The align argument is either LEFT, RIGHT, CENTER, LEADING, or TRAILING. These constants are defined in the SwingConstants interface, along with several others used by the Swing classes. The icon and text associated with the label can be read and written by the following methods:

     Icon getIcon()
     String getText()
     void setIcon(Icon i)
     void setText(String s)


Example:

/*
 * <APPLET CODE = "Swing01.CLASS" HEIGHT = 500 WIDTH = 500></APPLET>
 */
 import java.awt.*;
 import javax.swing.*;
public class Swing01 extends JApplet
{
          public void init()
          {
                   //Get Content Pane
                   Container contentPane = getContentPane();
                   ImageIcon ii = new ImageIcon("c:\\PracticeJava\\javalogo.gif");
                   JLabel jl = new JLabel("Java Logo", ii, JLabel.CENTER);
                   contentPane.add(jl);
          }
}


1 comments:


Thanks for sharing this good blog.This is very important and imformative blog for Java .It's very interesting and useful for students
Java Online Training Hyderabad

Post a Comment