使用Java的圆形Swing JButton

使用Java的圆形Swing JButton,java,swing,jbutton,Java,Swing,Jbutton,嗯,我有一个图像,我想把它作为一个按钮的背景(或一些陈词滥调的东西)。问题是这个图像是圆形的,所以我需要显示这个图像,没有任何边界,等等 保存此按钮的JComponent具有自定义背景,因此该按钮实际上只需要显示图像 在搜索谷歌之后,我无法做到这一点。我尝试了以下所有方法,但没有成功: button.setBorderPainted(false); button.setContentAreaFilled(false); button.setOpaque(true); 在我在背景中绘制图标后,按

嗯,我有一个图像,我想把它作为一个按钮的背景(或一些陈词滥调的东西)。问题是这个图像是圆形的,所以我需要显示这个图像,没有任何边界,等等

保存此按钮的JComponent具有自定义背景,因此该按钮实际上只需要显示图像

在搜索谷歌之后,我无法做到这一点。我尝试了以下所有方法,但没有成功:

button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setOpaque(true);
在我在背景中绘制图标后,按钮会绘制图标,但会保留一个带有边框的难看的灰色背景,等等。我还尝试使用JLabel和JButton。并在其上绘制图像图标,但如果用户调整窗口大小或最小化窗口,图标将消失

我怎样才能解决这个问题


我只需要画一个图像并将其环绕到一个JComponent上,然后聆听它的点击……

不透明度应该设置为false,所以

button.setOpaque(false);

可能已经是您想要的了。

您尝试过以下方法吗

button.setOpaque(false);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important
setboorder(null)
可能会起作用,但有人解释说,UI是通过设计在组件上设置边框的,除非客户端设置了非null边框,而非实现
UIResource
接口


当传入null时,与JDK本身将边框设置为
EmptyBorder
不同,客户端应该自己设置
EmptyBorder
(一个非常简单的解决方法)。这样,就不会混淆谁在代码中做什么。

我建议重写paint(Graphics g)方法,如下所示:

class JImageButton extends JComponent implements MouseListener {
    private BufferedImage img = null;

    public JImageButton(BufferedImage img) {
        this.img = img;
        setMinimumSize(new Dimension(img.getWidth(), img.getHeight()));
        setOpaque(false);
        addMouseListener(this);
    }

    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}
创建新的Jbutton:

    JButton addBtn = new JButton("+");
    addBtn.setBounds(x_pos, y_pos, 30, 25);
    addBtn.setBorder(new RoundedBorder(10)); //10 is the radius
    addBtn.setForeground(Color.BLUE);
为JButton设置边框时,调用重写的
javax.swing.border.border

addBtn.setBorder(new RoundedBorder(10));
这是上课时间

private static class RoundedBorder implements Border {

    private int radius;


    RoundedBorder(int radius) {
        this.radius = radius;
    }


    public Insets getBorderInsets(Component c) {
        return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
    }


    public boolean isBorderOpaque() {
        return true;
    }


    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        g.drawRoundRect(x, y, width-1, height-1, radius, radius);
    }
}
  • 将普通按钮拖动到面板上

  • 右键单击按钮并转到属性:

    boarder           = no barder
    boarder painted   = false
    contentAreaFilled = false
    focusPainted      = false
    opaque            = false
    
  • 通过导入到project来设置(图标)和(rolloverIcon)


  • 您可以尝试以下方法。它对我来说很好,我也面临着同样的按钮问题

    // Jbutton
    JButton imageButton = new JButton();
    
    // Buffered Icon
    BufferedImage buttonIcon = null;
    
    try {
        // Get the image and set it to the imageicon
        buttonIcon = ImageIO.read(getClass().getClassLoader().getResource("images/login.png"));
    }
    catch(Exception ex) {
    
    }
    
    // Set the image icon here
    imageButton = new JButton(new ImageIcon(buttonIcon));
    imageButton.setBorderPainted(false);
    imageButton.setContentAreaFilled(false);
    imageButton.setFocusPainted(false);
    imageButton.setOpaque(false);
    

    您可以为按钮创建一个空边框,如下所示:

    button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
    
    我写了一个类,可以处理椭圆形、圆形和胶囊状的按钮

    在您的情况下,扩展类并重写
    getBackgroundImage()
    方法以返回要设置为背景的图像。 然后像往常一样添加侦听器和文本。只有单击椭圆形/圆形区域才会触发该操作

    按钮类的示例:

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    
    public class ImageButton extends OvalButton {
    
        private BufferedImage image;
    
        public ImageButton() {
            super(); // Default is oval/circle shape.
    
            setBorderThickness(0); // Oval buttons have some border by default.
    
            try {
                image = ImageIO.read(new File("your_image.jpg")); // Replace with the path to your image.
            } 
            catch (IOException e) {
                e.printStackTrace();
                image = null;
            }
        }
    
        @Override
        protected BufferedImage getBackgroundImage() {
            return image;
        }
    }
    

    您可以查看类似问题的先前回答线索:您可以查看类似问题的先前回答线索:“寄宿生”?应该是“边界”吗?“没有吟游诗人”?应该是“无国界”吗?“寄宿生画”?应该是“boarder Paint”吗?@UVM这几乎是一个有用的评论。想解释一下为什么它“几乎正确”吗?可能是因为矩形背景的半径较大