【问题标题】:How to stop a JButton's height from changing after adding an underline?添加下划线后如何阻止JButton的高度发生变化?
【发布时间】:2017-02-17 14:52:44
【问题描述】:

所以我有一个JButton,当您将鼠标移到它上面时,会出现一个使用 HTML 的下划线。然而,当添加下划线时,它会使JButton 比它应该的要大一点。

我尝试了一些类似的方法

button.setMaximumSize(button.getPreferredSize());

在添加下划线之前,但我无法修复它。那么,无论是否存在下划线,我如何确保高度保持不变。

我在下面有一个示例类来演示问题(我无法让它具有完全相同的严重性,但足以显示问题)。

示例类

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.plaf.metal.MetalButtonUI;

public class UnderlineSample extends JFrame {
    public UnderlineSample() {
        super("Underline Sample");
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 150));

        String text =
                "<html>"
                    + "<p style = 'border-bottom: 1px solid rgba(0,0,0,0,0); box-sizing: border-box;'>"
                        + "<span style = 'color: rgb(0, 0, 255); font-weight: bold;'>"
                            + "Sample"
                        + "</span> "
                        + " underline button"
                    + "</p>"
               + "</html>";
        JButton button = new JButton(text);
        button.setFont(new Font("", Font.PLAIN, 18));
        button.setFocusable(false);
        button.setBackground(Color.LIGHT_GRAY);
        button.setForeground(Color.BLACK);
        button.setUI(new MetalButtonUI());
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setAlignmentX(LEFT_ALIGNMENT);

        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                String s =
                    "<html>"
                        + "<p style = 'border-bottom: 1px solid black; box-sizing: border-box;'>"
                            + "<span style = 'color: rgb(0, 0, 255); font-weight: bold;'>"
                                + "Sample"
                            + "</span> "
                            + " underline button"
                        + "</p>"
                   + "</html>";
                button.setText(s);
            }

            public void mouseExited(MouseEvent e) {
                String s =
                    "<html>"
                        + "<p style = 'border-bottom: 1px solid rgba(0,0,0,0,0); box-sizing: border-box;'>"
                            + "<span style = 'color: rgb(0, 0, 255); font-weight: bold;'>"
                                + "Sample"
                            + "</span> "
                            + " underline button"
                        + "</p>"
                   + "</html>";
                button.setText(s);
            }
        });

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST;
        gbc.weightx = 1; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.ipady = 10;
        gbc.insets = new Insets(0, 10, 5, 10);

        add(button, gbc);

        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new UnderlineSample();
    }
}

【问题讨论】:

    标签: java html jbutton preferredsize


    【解决方案1】:

    html/css 在 Swing 中的渲染并不支持所有的 css 样式。以rgba 为例,它不起作用(您可以通过使用rgba 添加一个彩色边框来自己测试它,您希望它被渲染而不是带有alpha 0 的边框并查看结果)。

    换句话说,带有rgba 的边框没有被渲染,因此当您添加带有MouseListener.mouseEntered 的可渲染边框底部时,大小会发生变化。您可以使用与背景颜色相同的rgb 来欺骗它。类似的东西:

    <p style = 'border-bottom: 1px solid rgb(200,200,200); box-sizing: border-box;'>
    

    【讨论】:

    • 谢谢,这成功了。然而,我确实有最后一个问题。尝试此操作时,我注意到 rgb 值之间的空格导致它不起作用。例如,虽然这有效:rgb(4,137,181);,但无效:rgb(4, 137, 181);。你知道这是为什么吗?
    • 很高兴它成功了。 I noticed that spaces between the rgb values caused it to not work 空白是 technically allowed - 这是 Swing css/html 解析器远非完美的证明。
    猜你喜欢
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2013-12-09
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多