使用 Java 的圆形 Swing JButton
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/423950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rounded Swing JButton using Java
提问by José Leal
Well, I have an image that I would like to put as a background to a button (or something clicable). The problem is that this image is round, so I need to show this image, without any borders, etc.
好吧,我有一个图像,我想将其作为按钮(或其他东西)的背景。问题是这个图像是圆形的,所以我需要显示这个图像,没有任何边框等。
The JComponent that holds this button has a custom background, so the button really needs to only show the image.
持有这个按钮的 JComponent 有一个自定义的背景,所以这个按钮真的只需要显示图像。
After searching Google, I couldn't manage to do so. I have tried all the following, but with no luck:
在谷歌搜索后,我无法这样做。我已经尝试了以下所有方法,但没有运气:
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setOpaque(true);
And after I paint the icon at the background, the button paints it, but holds an ugly gray background with borders, etc. I have also tried to use a JLabel and a JButton. And to paint an ImageIcon at it, but if the user resizes or minimizes the window, the icons disappear!
在我在背景上绘制图标后,按钮会绘制它,但保留了带有边框的丑陋灰色背景等。我还尝试使用 JLabel 和 JButton。并在其上绘制 ImageIcon,但如果用户调整窗口大小或最小化窗口,图标就会消失!
How can I fix this?
我怎样才能解决这个问题?
I just need to paint and round an image to a JComponent and listen for clicks at it...
我只需要绘制图像并将其四舍五入到 JComponent 并监听点击它...
回答by Bombe
Opacity should be set to false, so
不透明度应该设置为false,所以
button.setOpaque(false);
could already be what you want.
可能已经是你想要的了。
回答by VonC
Did you try the following?
您是否尝试过以下操作?
button.setOpaque(false);
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); // Especially important
setBorder(null)
might work, but there is a bug described at Sunexplaining it is by design that the UI sets a border on a component unless the client sets a non-null border which does not implement the UIResource
interface.
setBorder(null)
可能会起作用,但是Sun 描述了一个错误,说明它是设计使用户界面在组件上设置边框,除非客户端设置不实现UIResource
接口的非空边框。
Rather than the JDK itself setting the border to an EmptyBorder
when null is passed in, the clients should set an EmptyBorder
themselves (a very easy workaround). That way there is no confusion about who's doing what in the code.
EmptyBorder
当传入 null 时,不是 JDK 本身将边界设置为 ,而是客户端应该EmptyBorder
自己设置一个(一种非常简单的解决方法)。这样就不会混淆谁在代码中做什么。
回答by pek
I would recommend overriding paint(Graphics g) method as so:
我建议像这样覆盖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) {
}
}
回答by Lalchand
Create a new Jbutton:
创建一个新的 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);
while setting the border for a JButton, call the overridden javax.swing.border.Border
class.
在为 JButton 设置边框时,调用覆盖的javax.swing.border.Border
类。
addBtn.setBorder(new RoundedBorder(10));
Here is the class
这是课堂
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);
}
}
回答by Lalchand
Drag a normal button to your panel
Right click your button and go to properties:
boarder = no barder boarder painted = false contentAreaFilled = false focusPainted = false opaque = false
Set an (icon) and a (rolloverIcon) by importing to project.
将一个普通按钮拖到您的面板上
右键单击您的按钮并转到属性:
boarder = no barder boarder painted = false contentAreaFilled = false focusPainted = false opaque = false
通过导入到项目来设置 (icon) 和 (rolloverIcon)。
回答by Luffy
You can try the following. It works fine for me, and I also faced the same issue with the button.
您可以尝试以下操作。它对我来说很好用,我也遇到了与按钮相同的问题。
// 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);
回答by Reeba Babu
You can create an empty border to the button like this:
您可以像这样为按钮创建一个空边框:
button.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
回答by Luka Kralj
I wrote an OvalButtonclass that can handle oval, circular and capsule-like shaped JButtons.
我编写了一个OvalButton类,可以处理椭圆形、圆形和胶囊状 JButton。
In your case, extend the OvalButtonclass and override getBackgroundImage()
method to return the image you want to set as the background.
Then add listeners and text as usually. Only a click on the oval/circular area triggers the action.
在您的情况下,扩展OvalButton类和覆盖getBackgroundImage()
方法以返回要设置为背景的图像。然后像往常一样添加侦听器和文本。只有在椭圆形/圆形区域上单击才能触发操作。
Example of your button class:
您的按钮类示例:
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;
}
}