Results 1 to 6 of 6

Thread: JLabel - Swing Label Class

  1. #1
    Join Date
    Aug 2006
    Posts
    142

    JLabel - Swing Label Class

    I really want some help from you guys.!! I have recently started using the different classes in Java components. Now I have given an assignment for the Swing Label Class..!!I don't know anything about this class. Even I don't know the use of Swing Label Class in JLabel.?? Can someone explain me with some details.?? Also tell me when we use this class.? Expecting some needful help soon..!!
    ~HARE KRISHNA HARE RAM~

  2. #2
    Join Date
    Jul 2006
    Posts
    286

    Re: JLabel - Swing Label Class

    You should be knowing about the label first before looking at the coding of it. As the name suggest, you can guess that a label does not react to input events. So, it cannot get the keyboard focus. A JLabel object can display either text, an image. Sometimes it can display both text and an image. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. The position of the text relative to the image can also be specified by you. The text is on the trailing edge of the image, by default.
    IF you hate me, please don't mention it. I know who hates me and who doesn't. You really do not have to make fun of people....

  3. #3
    Join Date
    Nov 2008
    Posts
    996

    Re: JLabel - Swing Label Class

    JLabel is used exactly like Label as a way to display text. JLabel is frequently used as a way to put an image in a display because it can have an image instead of or in addition to the text. The ComponentOrientation property determines the label's leading and trailing edge. Also the default ComponentOrientation setting maps the leading edge to left and the trailing edge to right. If you want to set the pixels between the text and the image, you can use the setIconTextGap method.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: JLabel - Swing Label Class

    If you need to create a component that displays a string, an image, or both, you can do so by using or extending JLabel. By specifying HTML code in a label's text, you can give the label various characteristics such as multiple lines, multiple fonts or multiple colors. The three features of JLabel different from the Label are :
    • The ability to display images.
    • The ability to place borders around the labels.
    • The ability to use HTML to format the label.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: JLabel - Swing Label Class

    The following codes explains the JLabel with HTML :
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class JLabels extends JFrame {
      public static void main(String[] args) {
        new JLabels();
      }
    
      public JLabels() {
        super("HTML used in JLabels");
        WindowUtilities.setNativeLookAndFeel();
        addWindowListener(new ExitListener());
        Container content = getContentPane();
        Font font = new Font("Georgia", Font.PLAIN, 20);
        content.setFont(font);
        String labelText =
          "<html><FONT COLOR=BLUE>Blue</FONT> and " +
          "<FONT COLOR=YELLOW>Yellow</FONT> Text</html>";
        JLabel coloredLabel =
          new JLabel(labelText, JLabel.CENTER);
        coloredLabel.setBorder
          (BorderFactory.createTitledBorder("Mixed Colors"));
        content.add(coloredLabel, BorderLayout.NORTH);
        labelText =
          "<html><B>Bold</B> and <I>Italic</I> Text</html>";
        JLabel boldLabel =
          new JLabel(labelText, JLabel.CENTER);
        boldLabel.setBorder
          (BorderFactory.createTitledBorder("Mixed Fonts"));
        content.add(boldLabel, BorderLayout.CENTER);
        labelText =
          "<html>Unity in Diversity " +
          "can be seen in this Country." +
          "<P>" +
          "The Oceans are :" +
          "<UL>" +
          "  <LI>Atlantic" +
          "  <LI>Pacific" +
          "  <LI>Indian" +
          "  <LI>Artic" +
          "</UL>";
        JLabel fancyLabel =
          new JLabel(labelText,
                     new ImageIcon("images/pic51.gif"),
                     JLabel.CENTER);
        fancyLabel.setBorder
          (BorderFactory.createTitledBorder("Contains Many Lines HTML"));
        content.add(fancyLabel, BorderLayout.SOUTH);
        pack();
        setVisible(true);
      }
    }

  6. #6
    Join Date
    Aug 2006
    Posts
    168

    Re: JLabel - Swing Label Class

    I am providing you with the sample of the code which would be useful for you to create the labels :
    Code:
    ImageIcon icon = createImageIcon("picture12.gif");
    . . .
    label1 = new JLabel("Image and Text",
                        icon,
                        JLabel.CENTER);
    //Set the position of the text, relative to the icon:
    label1.setVerticalTextPosition(JLabel.BOTTOM);
    label1.setHorizontalTextPosition(JLabel.CENTER);
    
    label2 = new JLabel("Text-Only Label");
    label3 = new JLabel(icon);
    Hope that it helps..!!
    "All gave some, some gave all."

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. Problem of overlapping JLabel on JFrame with background
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 14-12-2010, 02:09 PM
  3. Where can I find Java 1.5 swing (JTabbedPane class) source code
    By Who is it in forum Software Development
    Replies: 5
    Last Post: 23-07-2010, 03:35 AM
  4. Display An Image In A JLabel
    By Amaresh in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 05:56 PM
  5. How to use Swing Timer class in java program?
    By KALANI84 in forum Software Development
    Replies: 4
    Last Post: 25-01-2010, 04:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,550,905.68984 seconds with 17 queries