【问题标题】:Adding buttons with multi line text but both lines having center paragraph alignment添加带有多行文本但两行都具有中心段落对齐的按钮
【发布时间】:2018-12-11 21:10:19
【问题描述】:

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

下面是我生成这个按钮的代码(我已经包装了文本,所以它看起来不难看)。是否可以让第一行 "View Config File" 居中对齐,同时保持第二行居中对齐?

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

【问题讨论】:

    标签: java html swing jbutton


    【解决方案1】:

    最初的建议是使用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

    【讨论】:

    • @Pete,谢谢,我只知道 HTML 基础知识。答案已更新。
    • 啊不确定它是否转换了标签 - 你的 html 需要是 style="text-align:center"
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 2017-08-22
    • 2013-04-30
    • 1970-01-01
    • 2015-08-23
    • 2021-12-16
    • 2015-02-02
    • 2020-12-26
    相关资源
    最近更新 更多