Java 如何在matteborder中的图标周围添加空白

Java 如何在matteborder中的图标周围添加空白,java,swing,Java,Swing,我尝试了MatteBorder,以便在JTextField的开头显示图标(类似于搜索文本字段中显示的图标) 这是我当前的实现: JTextField textField = new JTextField("Filter", 8); textField.setPreferredSize(new Dimension(getPreferredSize().width, 24)); Border outer = textField.getBorder(); // ugly workaround Bord

我尝试了
MatteBorder
,以便在
JTextField
的开头显示图标(类似于搜索文本字段中显示的图标)

这是我当前的实现:

JTextField textField = new JTextField("Filter", 8);
textField.setPreferredSize(new Dimension(getPreferredSize().width, 24));
Border outer = textField.getBorder();
// ugly workaround
Border padding = BorderFactory.createEmptyBorder(2, 2, 2, 2);
Border search = new MatteBorder(0, 16, 0, 0, icon);
textField.setBorder(new CompoundBorder(new CompoundBorder(outer, padding), search));
该图标是一个16x16px的图标,周围没有空格。文本字段为24px高。我引入了一个填充边框,在图标周围有一些空白(否则它将显示一个完整的图标和图标下的第一个4px)。我的问题是图标右侧没有空格(用户输入文本的地方)

问题:有没有办法在图像周围定义一定数量的空白,这样它就不会平铺?在将图标添加到
MatteBorder
之前,是否可以在图标周围添加“填充”

另外,我知道我可以在图像文件周围添加空白,但在其他情况下,它不应该在图像文件周围添加任何空白

我的问题是图标右侧没有空格(用户输入文本的地方)

使用第二个
CompoundBorder
在边框内侧添加额外的空间如何

另一种可能性是使用,它允许您向边框添加组件。因此,您可以添加带有图标的JLabel

我的问题是图标右侧没有空格(用户输入文本的地方)

使用第二个
CompoundBorder
在边框内侧添加额外的空间如何


另一种可能性是使用,它允许您向边框添加组件。因此,您可以添加带有图标的JLabel。

因为MatteBorder总是平铺图标,所以我不会使用它。我只想写一个自定义边框:

static void updateBorder(JTextField textField,
                         Icon icon) {

    Border iconBorder = new AbstractBorder() {
        private static final long serialVersionUID = 1;

        @Override
        public Insets getBorderInsets(Component c,
                                      Insets insets)
        {
            insets.left = icon.getIconWidth() + 4;
            insets.right = insets.top = insets.bottom = 0;
            return insets;
        }

        @Override
        public void paintBorder(Component c,
                                Graphics g,
                                int x,
                                int y,
                                int width,
                                int height)
        {
            icon.paintIcon(c, g,
                x, y + (height - icon.getIconHeight()) / 2);
        }
    };

    Border oldBorder = textField.getBorder();

    // Inside text field's original border, place icon border
    // with a little empty space around it.
    textField.setBorder(BorderFactory.createCompoundBorder(
        oldBorder,
        BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(2, 2, 2, 2),
            iconBorder)));
}

因为MatteBorder总是平铺图标,所以我不会使用它。我只想写一个自定义边框:

static void updateBorder(JTextField textField,
                         Icon icon) {

    Border iconBorder = new AbstractBorder() {
        private static final long serialVersionUID = 1;

        @Override
        public Insets getBorderInsets(Component c,
                                      Insets insets)
        {
            insets.left = icon.getIconWidth() + 4;
            insets.right = insets.top = insets.bottom = 0;
            return insets;
        }

        @Override
        public void paintBorder(Component c,
                                Graphics g,
                                int x,
                                int y,
                                int width,
                                int height)
        {
            icon.paintIcon(c, g,
                x, y + (height - icon.getIconHeight()) / 2);
        }
    };

    Border oldBorder = textField.getBorder();

    // Inside text field's original border, place icon border
    // with a little empty space around it.
    textField.setBorder(BorderFactory.createCompoundBorder(
        oldBorder,
        BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(2, 2, 2, 2),
            iconBorder)));
}
1) 为了更快地获得更好的帮助,请发布一个or。2) 例如,获取图像的一种方法是热链接到中所示的图像。1)要更快获得更好的帮助,请发布或。2) 例如,获取图像的一种方法是热链接到中看到的图像。