JLabel Center Content in 3 lines

I am trying to center the connections inside JLabel and add the first three lines to three different lines. I tried to do this using CSS, but JLabel did not format it as I wanted. At the moment, it looks like this:
enter image description here


But I would like it to look something like this:

==================================
|| Eleking02 vs. Nils Eleking02 vs. Nils ||
||
|| . 7 - 3 .
||
|| . Gewonnen! .



 pane.setText("<html><div style=\"color: green; text-align: left;\">" + this.getGewinner() + "</div><div style=\"text-align: center; margin-top: -17px;\">" + "Vs. </div><div style=\"margin-top: -20px; text-align: right; color: red;\">" + this.getVerlierer() + "</div><br/>" + "<font>" + this.getSiegerPunkte() + " - " + this.getVerliererPunkte() + "</font><br/>" + "<font>Gewonnen!</font></html>"); 

Maybe I can achieve what I want with gridLayout?

+5
source share
1 answer

It's amazing that you can use HTML / CSS for Java shortcut styles. There may be several ways to solve this problem, but if you want to use HTML / CSS, here is one way that seemed to work:

 pane.setText("<html>" + "<div style=\"text-align: center;\">" + "<span style=\"color: green; text-align: left;\">" + "Eleking02 " + "</span>" + "<span style=\"text-align: center; margin-top: -17px;\">" + "Vs. </span> " + "<span style=\"margin-top: -20px; text-align: right; color: red;\">" + " Nils" + "</span>" + "</div>" + "<br/>" + "<div style=\"text-align: center;\">" + "<font>" + "7" + " - " + "3" + "</font>" + "</div>" + "<br/>" + "<div style=\"text-align: center;\">" + "<font>Gewonnen!</font></html>" + "</div>" + "</html>"); 

Image of JLabel with formatting from CSS / HTML

HTML div elements are block elements, which means that they automatically create a new line after each div. You had several Divs, and that would prevent the text you want everything to be on the same line. The span element does the same, except that it does not create a new line after the element. To center the text, I just placed what I wanted to concentrate inside the Div and apply text-align: center. It seemed like a trick.

0
source

All Articles