Java
/
Swing
- 1 Basics 9
-
Classes
-
Objects
-
Arrays
-
Variables
-
Loops
-
Numbers
-
Strings
-
Exceptions
-
Regexp
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static
-
Abstract
-
Interfaces
-
Constructors
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread
-
Sleep
-
Lock
-
Scheduler
- 6 Design patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt
- 9 Effective 7
-
Constructors
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 8
-
Quick start
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
/
Label
➟
➟
Last update: 26-07-2021
JLabel
With the JLabel you can display unselectable text and images. Use font attributes to set JLabel underline.
import java.awt.*;
import java.awt.font.TextAttribute;
import java.util.Map;
import javax.swing.*;
public class App extends JFrame {
public static void main(String[] args) {
App frame = new App();
frame.setBounds(200, 200, 300, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public App() {
JLabel label = new JLabel("Underlined Label");
Font font = label.getFont();
Map attributes = font.getAttributes();
// Look Here
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
//attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
//attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
label.setFont(font.deriveFont(attributes));
JPanel panel = new JPanel();
panel.add(label);
add(panel);
}
}
Border
We can set a border to label using setBorder() method.
import java.awt.*;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class App extends JFrame {
public static void main(String[] args) {
JFrame frame = new App();
frame.setBounds(200, 200, 300, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
public App() {
JLabel label = new JLabel("my text"); // Look Here
label.setBorder(BorderFactory.createLineBorder(Color.red));
JPanel panel = new JPanel();
panel.add(label);
add(panel);
// Outputs: a label with red border
}
}
➥ Questions