使用Java圆角Swing JButton

时间:2009-01-08 11:25:30

标签: java swing jbutton

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

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

搜索谷歌后,我无法做到这一点。我已经尝试了以下所有方法,但没有运气:

button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setOpaque(true);

在我在背景上绘制图标后,按钮会绘制它,但是会保留带有边框的丑陋灰色背景等。我还尝试使用JLabel和JButton。并在其上绘制ImageIcon,但如果用户调整窗口大小或最小化窗口,图标就会消失!

我该如何解决这个问题?

我只需要将图像绘制并舍入到JComponent并听取它的点击次数......

8 个答案:

答案 0 :(得分:15)

创建一个新的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);
    }
}

答案 1 :(得分:12)

您是否尝试过以下操作?

button.setOpaque(false);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important

setBorder(null)可能有效,但有a bug described at Sun解释说,按设计,UI会在组件上设置边框,除非客户端设置非空边框,而不实现{{1接口。

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

答案 2 :(得分:1)

我建议覆盖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) {
    }
}

答案 3 :(得分:1)

  1. 将普通按钮拖到面板上

  2. 右键单击按钮,然后转到属性:

    boarder           = no barder
    boarder painted   = false
    contentAreaFilled = false
    focusPainted      = false
    opaque            = false
    
  3. 通过导入项目设置(icon)和(rolloverIcon)。

答案 4 :(得分:1)

您可以尝试以下操作。它对我来说很好,我也遇到了与按钮相同的问题。

<xsl:value-of select="document('file2.xml')/R/Rt/W/AL"/>

答案 5 :(得分:0)

不透明度应设为false,所以

button.setOpaque(false);

可能已经是你想要的了。

答案 6 :(得分:0)

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

//VerticalSpace from SmallFont Label to largeFont Label
@IBOutlet weak var dollarLabelTopSpacetoAmountLabel: NSLayoutConstraint!

@IBAction func btnClicked() {
    let amountString = self.amountLbl.text! as NSString
    let fontSize =  self.amountLbl.frame.size.width / CGFloat(amountString.length)
    let difference = self.amountLbl.frame.size.height-fontSize
    print(fontSize)
   //Your minimum font size(30).
    if fontSize>30 {
        self.dollarLabelTopSpacetoAmountLabel.constant = -(difference-
    }else{
        // dollarLabelTopSpacetoAmountLabel.constant = -30
    }

答案 7 :(得分:0)

我写了一个OvalButton类,可以处理椭圆形,圆形和类似胶囊形状的JButton。

在您的情况下,扩展OvalButton类并重写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;
    }
}