What is jlabel in java swing




















We can simply use JLabel by creating and using an instance for this class. You can simply write it as:. JLabel does not react to input events performed by the user like mouse focus or keyboard focus.

It is simply a non-editable text or image or icon or both. JLabel is generally used along with those components that do not have their own ability to explain or demonstrate their purpose. For example, a text area for entering a Name or Password, etc will require a label to tell the user about the text box. Without the usage of JLabel, the text boxes will appear lost to a user since they do not tell themselves what the user is expected to enter in the text field. Take the following example, we have added a text field without any labels.

This was a simple example we created. It was a simple program displaying a text field and a label with it. We also can add an icon along with using another commonly used method with JLabel, known as the setIconTextGap method.

This method helps the programmer to specify how many pixels our text and our image should be displayed apart. Java JLabel class has several constructors that can be used to create our label with different characteristics. You can see the code below, I have used Eclipse for writing the code. JLabel String s : creates a new label with the string specified. JLabel Icon i : creates a new label with a image on it.

JLabel String s, Icon i, int align : creates a new label with a string, an image and a specified horizontal alignment Commonly used methods of the class are : Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

Program to create a blank label and add text to it. Program to create a label and add image to it. Skip to content. Change Language. Related Articles. Table of Contents.

Run the program using the following command. Previous Page. Next Page. Previous Page Print Page. Save Close. Dashboard Logout. JLabel Creates a JLabel instance with no image and with an empty string for the title.



0コメント

  • 1000 / 1000