- BASICS
- Classes
- Objects
- Arrays
- Variables
- Loops
- Numbers
- Strings
- Exceptions
- Regexp
- OOP
- Inheritance
- Polymorphism
- Static Keyword
- Abstract Keyword
- Interfaces
- Constructors
- Packages
- Nested Classes
- Final Keyword
- SWING
- Frame
- Panel
- Listener
- Combo Box
-
Label
- Image
- Menu
- Table
- Layout
- Drawing
- Timer
- Designer
- COLLECTIONS
- Lists
- Comparable
- Sets
- Maps
- Generics
- Properties
- Streams
- Json
- COMPILER
- Sublime Text
- Apache Ant
- I/O
- Streams IO
- Socket
- Watching Files
- Logger
- Clipboard
- Encrypt
- JAVAFX
- Openjfx
- Scene Builder
- First App
- Jar Archive
- On Action
- Change Listener
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
}
}
Last update: 546 days ago