How can I format a JLabel
and its contents with different "properties"?
For example, I'd like to be able to set a color for the text of my JLabel
, and another for string
that is concatenated with it. It's possible?
In my case, it only got a "way", and the% w_that I tried to use did not work either.
What I've tried:
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Teste extends JFrame {
public static void main(String[] args) {
new Teste().setVisible(true);
}
private JPanel painel = new JPanel();
private Font fonte = new Font("SansSerif", Font.BOLD, 15);
private JLabel label = new JLabel();
private String string = "string";
public Teste() {
setSize(500, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
painel.add(label);
add(painel);
label.setText("Label + " + "<html><font color=#166B44> " + string + " </font></html>");
label.setFont(fonte);
label.setForeground(Color.red);
}
}