Vertically centering text in HTML table cell in Java JLabel

喜夏-厌秋 提交于 2019-11-26 10:03:24

问题


I have an HTML table (styles with CSS) that is displayed in a JLabel. I would like the contents (a single, short line of text) of the cells to be both horizontally and vertically centered. Horizontal centering is easy, but I cannot seem to center the text vertically. I\'ve tried vertical-align: and valign: with both middle and center arguments. I\'ve looked at several of the tricks, but none of them seemed doable, and the ones I tried didn\'t work.

What I have now:

Inline (in the <style> tag) CSS:

table.outer {
    background:#F0F0F0;
    border-collapse:collapse;
    border:none;
}

td.outer {
    border-style:solid;
    border-width:1pt;
    border-color:#888888;
    padding:0pt 0pt 0pt 0pt;
}

table.inner {
    border-collapse:collapse;
    border:none;
}

td.title {
    width:75pt;
    background:#BFBFBF;
    padding:1.5pt 0pt 1.5pt 0pt;
}

The relevant portion of the HTML:

<body bgcolor=#F0F0F0>
<table class=\'outer\'>
  <tr>
    <td class=\'outer\' valign=top>
    <table class=\'inner\'>
      <tr>
        <td class=\'title\' rowspan=3>
          <p class=\'title\'>Current Run</p>
        </td>
      </tr>
    </table>
    </td>
  </tr>
</table>
</body>

回答1:


Support for HTML in Swing Components is limited to 3.2, but valign="middle" should work.



来源:https://stackoverflow.com/questions/6784577/vertically-centering-text-in-html-table-cell-in-java-jlabel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!