The way to display text or prompt information in Swing is to use labels, which support text strings and icons. In the user interface of an application, a short text label can let users know the purpose of these components, so labels are commonly used in Swing. The usage of Swing tags, how to create tags, and how to place text and icons on tags.
Use of labels
The tag is defined by the JLabel class, whose parent class is the JComponent class.
Labels can display a line of read-only text, an image or text with an image. They do not produce any kind of event. They simply display text and images, but they can specify the alignment of text on the label using the characteristics of the label.
The JLabel class provides a variety of constructions to create multiple tags, such as text-only tags, icon-only tags, or tags containing text and icons.
Several commonly used constructions of JLabel classes are as follows.
public JLabel(): Create a JLabel object without icons and text. public JLabel(Icon icon): Create a JLabel object with icons. public JLabel(Icon icon,int aligment): Create a JLabel object with icons and set the horizontal alignment of icons. public JLabel(String text,int aligment): Create a JLabel object with text and set the text level alignment. Public JLabel (String text, Icon icon, int aligment): Create a JLabel object with text and icon, and set the horizontal alignment of label content.
Use of icons
Icons in Swing can be placed on buttons, labels and other components to describe the use of components. Icons can be created using the image file type supported by Java, or using the functional methods provided by the java.awt.Graphics class.
Create icons
Icon interface is used to create icons in swing. The size and color of icons can be given at the time of creation. If you use the Icon interface, you must implement the following three methods in the Icon interface:
public int getIconHeight()
public int getIconWidth()
public void paintIcon(Component arg0, Graphics arg1, int arg2 , int arg3)
The getIconHeigth() and getIconWidth() methods, as their names imply, are the methods for obtaining the length and width of a graph. The paintlcon() method is used to draw a graph at a specified coordinate position.
The Drawlcon class that implements the Icon interface is created in the project, which implements the custom icon class.
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class DrawIcon implements Icon{ //Implementing Icon Interface
private int width; //Declare the width of the icon
private int height; //The length of the declaration Icon
public DrawIcon(int width,int height){ //Define the construction method
this.width=width;
this.height=height;
}
public DrawIcon(){ //Spatial Construction Method
}
//Implementing paintIcon() method
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height); //Draw a circle
}
@Override
public int getIconWidth() { //Implementation of getIconWidth() method
return this.width;
}
@Override
public int getIconHeight() { //Implementing getIconHeight() method
return this.height;
}
public static void main(String[] args) {
DrawIcon icon=new DrawIcon(15,15);
//Create a label and set the text on the label in the middle of the label
JLabel jl=new JLabel("test",icon,SwingConstants.CENTER);
JFrame jf=new JFrame("window");
Container c=jf.getContentPane();
c.add(jl);
jf.setSize(500,500);
jf.setLocationRelativeTo(jf);
jf.setVisible(true);
}
}
Result:
Analysis:
Since the Drawlcon class inherits the Icon interface, all the methods defined in the Icon interface must be implemented in this class. In the implementation of paintIcon(), the method in the Graphics class is used to draw a circular icon, and the other methods to implement the interface are to return the length and width of the icon. The length and width of the icon are set in the construction method of the Drawlcon class, so that if you need to use the icon in the form, you can use the following code
DrawIcon icon=new DrawIcon(15,15);
In general, the icon will be placed on the button or label, where the icon will be placed on the label, and then the label will be added to the container, thus realizing the function of using the icon in the form.
Use picture icons
In addition to being able to draw icons in Swing, they can also be created using a specific image. Swing uses javax.swing.ImageIcon class to create icons based on existing pictures, ImageIcon class to create icons based on existing pictures, Imagelcon class implements Icon interface, and Java supports a variety of image formats.
The ImageIcon class has several constructive methods, several of which are commonly used as follows.
public ImageIcon(): This constructor creates a generic Imagelcon object, which calls the setlmage (lmage mage) method to operate when the image really needs to be set. Public Image Icon (Image image): Icons can be created directly from the image source. Public lmage image (String description): In addition to creating an icon from the image source, you can also add a short description of the icon, but this description will not be displayed on the icon, you can use the getDescription() method to get the description. public Imagelcon(URL url): This construct uses image files located on a computer network to create icons.
The Drawlcon class that implements the Icon interface is created in the project, which implements the custom icon class.
import java.awt.Container;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class MyImageIcon extends JFrame{
public MyImageIcon(){
Container container = getContentPane();
JLabel jl=new JLabel("This is a JFrame forms",JLabel.CENTER); //Create a label
Icon icon=new ImageIcon("image.jpeg"); //Instantiating Icon objects
jl.setIcon(icon); //Set up pictures for labels
jl.setHorizontalAlignment(SwingConstants.CENTER); //Set text in the middle of the label
jl.setOpaque(true); //Set labels to opaque state
container.add(jl); //Add labels to containers
setSize(250,100); //Setting Form Size
setVisible(true); //Set Form Visibility
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Setting Form Close Mode
}
public static void main(String[] args) {
new MyImageIcon(); //Instance object
}
}
Result:
Example
Title: Adding illustrations to pictures
Create the UseLabelFrame class in the project, which inherits from the JFrame class to form class, and then create tag components that display text and images, respectively.
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class UserLabelFrame extends JFrame{
public UserLabelFrame(){
super(); //Call the construction method of the parent class
JFrame jf=new JFrame("Using Label Components"); //Setting Form Title
jf.setSize(500,500); //Set the size of the form
jf.setLocationRelativeTo(jf); //Centralize the form
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Setting the Closing Mode of Forms
Container container=jf.getContentPane(); //Create a container
final JLabel jl=new JLabel("This is a beautiful little girl."); //Create a label component and add a title
container.add(jl,BorderLayout.NORTH); //Add the label to the north of the form
Icon icon=new ImageIcon("image.jpeg"); //Create a picture Icon
final JLabel jl2=new JLabel(); //Create small containers
jl2.setIcon(icon); //Put the icon in a small container
container.add(jl2,BorderLayout.CENTER); //Place small containers in the center of the form
jf.setVisible(true); //Set the form to be visible
}
public static void main(String[] args) {
new UserLabelFrame();
}
}
Result: