Вопрос

int variable = 100;

label = new JLabel("<html><font color=red>variable</font><html>");

How do I make this display "100" on screen and not "variable"

Это было полезно?

Решение

By building a String with it included:-

label = new JLabel("<html><font color=red>" + variable + "</font><html>");

This will output <html><font color=red>100</font><html> (well, in a label it will be formatted) The reason yours didn't work is because almost anything inside quotes is treated as a String so adding a variable in there is just the same as adding the name of the variable.

Additionally

This will also work for objects, not just primitive types like int by calling their toString() method and adding the output from that.

Другие советы

You can use String.format method:

label = new JLabel(String.format("<html><font color=red>%d</font><html>", variable));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top