【问题标题】:Jbutton invisible border with padding带填充的 Jbutton 不可见边框
【发布时间】:2018-03-07 05:39:45
【问题描述】:

我有一个带有 ImageIcon 和一些文本的 JButton。 我想要有没有边框的透明背景,但也想要有一些填充。

这是我尝试过的:

Jbutton button = new JButton();

//Add image to the button
ImageIcon img= new ImageIcon(imgUrl);
button.setIcon(img);

//make button transparent
button.setBackground(new Color(255,255,255,0));

//Remove border
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);

//add padding
button.setBorder(BorderFactory.createEmptyBorder(5,10,5,50));

我仍然看到一个接一个的灰色边框。 当我做 button.setBorder(null) 时,灰色边框线消失了,但是我无法添加填充。

如果有人可以指导我做错了什么。我对摇摆很陌生,尝试过不同的答案,但都没有奏效。

谢谢。

【问题讨论】:

  • button.setBackground(new Color(255,255,255,0)); 不是让组件透明的方式,相反,您需要设置 setOpaque 并将其传递为 false
  • 我还想看看 JButton#setMargin 添加填充
  • @MadProgrammer 我尝试了 button.setMargin(new Insets(10,10,10,10)) 但无法将边框设置为空。还有什么我可以尝试的吗?
  • 对我来说似乎工作正常
  • 使用MatteBorder 而不是EmptyBorder。例如button.setBorder(new MatteBorder(10, 10, 10, 10, color)),其中的颜色是所需的背景。

标签: java swing


【解决方案1】:
  • 不要使用setBackground(new Color(255,255,255,0)),Swing 仅支持完全不透明或完全透明的组件,通过 opaque 属性控制,使用基于 alpha 的颜色会产生不良副作用
  • 考虑使用setMargins 生成填充

示例...

原图...

import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Launcher {

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

    public Launcher() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JButton button = new JButton();

                    ImageIcon img = new ImageIcon(ImageIO.read(getClass().getResource("Cup.png")));
                    button.setIcon(img);
                    button.setBorderPainted(false);
                    button.setContentAreaFilled(false);
                    button.setFocusPainted(false);
                    button.setOpaque(true);
                    button.setMargin(new Insets(10, 10, 10, 10));
                    JFrame frame = new JFrame();
                    // Just testing to see there are glitches
                    frame.setLayout(new GridBagLayout()); 
                    frame.add(button);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Launcher.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}

我确实发现如果您使用 setBorder,它会覆盖 margin

【讨论】:

    【解决方案2】:

    如何将边距设置为布局管理器上的任何摆动组件。

    如果您使用的是 GridBagLayout,则应将 GridBagConstraint 对象上的 insets 设置为所需的值。

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 2022-01-11
      • 2015-07-26
      • 1970-01-01
      • 2012-08-04
      • 2012-09-12
      • 2022-08-05
      • 1970-01-01
      相关资源
      最近更新 更多