简体   繁体   English

删除自定义按钮上的边框

[英]Removing borders on custom buttons

I've written this small program which attempts to create a custom JButton unfortunately I can't manage to remove the border. 我写了这个小程序试图创建一个自定义JButton不幸的是我无法设法删除边框。 I thought button.setBorder(null); 我以为button.setBorder(null); would remove it but this has been ineffective. 会删除它,但这是无效的。 Does anyone know how to remove the border from the button so it's just the icon? 有谁知道如何从按钮中删除边框,所以它只是图标? Any help greatly appreciated. 任何帮助非常感谢。

My code is as follows: 我的代码如下:

package custombuttons;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
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.JPanel;

public class CustomButtons {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        CustomButtons h = new CustomButtons();
        h.setUp();
    }

    JFrame frame;
    JPanel panel;
    JButton button;
    BufferedImage b;
    String toolTip = "Configure";

    public void setUp() {
        frame = new JFrame("Custom Buttons");
        try {
            b = ImageIO.read(CustomButtons.class.getResource("/images/config.png"));
        } catch (IOException ex) {
            Logger.getLogger(CustomButtons.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }

        Image b1 = (Image) b;
        ImageIcon iconRollover = new ImageIcon(b1);
        int w = iconRollover.getIconWidth();
        int h = iconRollover.getIconHeight();
        GraphicsConfiguration gc = frame.getGraphicsConfiguration();
        Image image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.drawImage(iconRollover.getImage(), 0, 0, null);
        g.dispose();

        ImageIcon iconDefault = new ImageIcon(b1);
        image = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        g = (Graphics2D) image.getGraphics();
        g.drawImage(iconRollover.getImage(), 2, 2, null);
        g.dispose();
        ImageIcon iconPressed = new ImageIcon(b1);

        JButton button = new JButton();
        button.setIgnoreRepaint(true);
        button.setFocusable(false);
        button.setToolTipText(toolTip);
        button.setBorder(null);
        button.setContentAreaFilled(false);
        button.setIcon(iconDefault);
        button.setRolloverIcon(iconRollover);
        button.setPressedIcon(iconPressed);

        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));

        panel = new JPanel();
        panel.setOpaque(false);
        panel.add(button);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }
}

在这里看一下关于JButton的 button.setBorderPainted(false)

actually i tested your code on my Netbeans IDE and got no borders as you desire using only button.setBorder(null); 实际上,我在我的Netbeans IDE上测试了你的代码,没有你想要的边框,只使用button.setBorder(null); or button.setBorderPainted(false); button.setBorderPainted(false); or both of them but i think you should make sure that your original image is really do not have any borders it self both of them但我认为你应该确保你的原始图像真的没有自己的边界

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM