添加具有多行文本但两行都具有中心段落对齐的按钮

Adding buttons with multi line text but both lines having center paragraph alignment

我正在尝试创建一个包含两行文本的按钮。使用 <html> 中的 <br/> 标签很容易做到这一点,但是较大的线居中对齐但较小的线向左浮动:

下面是我生成此按钮的代码(我已将文本换行,因此看起来不难看)。是否可以让第一行 "View Config File" 居中对齐,同时保持第二行居中对齐?

    JButton viewConfigFile = new JButton("<html>View Config File" + 
    "<br/>Be careful of any changes made</html>");

最初的建议是使用 center 标签来包裹文本,但 Pete 指出此标签已过时且在 HTML 4 中已弃用(因此我不建议使用此标签)。

JButton button = new JButton("<html><center>View Config File" 
    + "<br/>Be careful of any changes made</center></html>");

您可以使用 CSS 文本对齐 属性:

JButton button = new JButton("<html><div text-align:center>View Config File" + "<br/>Be careful of any changes made</div></html>");

参见:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/center