Swing is a toolkit for creating a graphical user interface (GUI) in the Java programming language. It is part of the Java Foundation Classes (JFC) library.

Swing was developed to provide a more functional set of program components for creating a graphical user interface than the previous AWT toolkit. Swing components support specific look-and-feel modules that are dynamically connected. Thanks to them, it is possible to emulate the graphical interface of the platform (that is, you can dynamically connect other, specific for a given operating system look and feel to the component). The main disadvantage of such components is their relatively slow operation, although this has not been confirmed recently due to the growth of personal computers. The positive side is the universality of the interface of the created programs on all platforms.

JLabel

This is the simplest component available in Java Swing. With JLabel, you can display text with an icon. If you need a component to display a message to the user, or to make a text label for the input field, or to show an icon, use JLabel. The text that the JLabel displays cannot be selected, only viewed.

To create a JLabel object, which we will then have at our disposal in the window, you can use the constructor with a string parameter
public JLabel (String text)

The string parameter is the text that will be displayed in the JLabel. In addition, the text that will be displayed in the JLabel can be set using the setText method. The only parameter of the method is the string of text to be displayed String.

JLabel allows you to customize the font that will be used to display the text. The font is set using the
setFont method of the JLabel class
This method is passed a Font object as a parameter.

What do we want when we specify or create a font?

  • The first thing is the typeface, Verdana or Courier New, or something else;
  • The second is the font size and;
  • third – whether the font will be bold or normal, or maybe italicized.

Therefore, the appropriate constructor for Font is the one that contains all three parameters at once – public Font (String name, int style, int size). The first parameter, as is already clear, is the name of the font. If this parameter is not specified, the default font is used. The second parameter sets the style – bold, normal, or italic. Here you need to pass one of the constants of the Font object – Font.BOLD (bold), Font.PLAIN (normal or flat) and Font.ITALIC (italic). And the third parameter is the font size. The Font object can be created like this:

Font font = new Font("Verdana", Font.PLAIN, 11);

After setting the text and font in JLabel, sometimes you need to determine the vertical and horizontal alignment of the text. This is done using two methods: setVerticalAlignment and setHorizontalAlignment, respectively. As parameters for setVerticalAlignment, you need to use something from the following list:

  • JLabel.TOP (align to the top edge),
  • JLabel.CENTER, and
  • JLabel.BOTTOM (align to the bottom edge).
  • For the setHorizontalAlignment method, there are more parameters –
  • JLabel.LEFT (left alignment),
  • JLabel.CENTER (centered),
  • JLabel.RIGHT (on the right edge),
  • JLabel.LEADING and
  • JLabel.TRAILING.

The last two constants are interesting. For languages in which the text is written from left to right, JLabel.LEADING is the left edge, and JLabel.TRAILING is the right edge. For languages that are written from right to left, JLabel.LEADING is the right edge and JLabel.TRAILING is the left edge.

JLabel allows you to display an icon along with the text. To set the icon, use the setIcon method. The only parameter of the method is the Icon object. When an icon is set for the JLabel, you can set the location of the text relative to the icon. To do this, use the setVerticalTextPosition and setHorizontalTextPosition methods. The same constants are used as parameters as when aligning text.