java 如何在java中创建圆形JButton ..?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5120116/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:32:45  来源:igfitidea点击:

How to create rounded JButton in java..?

javaswingjbuttonrounded-cornerslook-and-feel

提问by mani

I want to create rounded JButton in Java...
For that I use rounded image and placed that image on button but I didn't get rounded button..

我想在 Java 中创建圆形 JButton ......
为此,我使用圆形图像并将该图像放在按钮上,但我没有得到圆形按钮......

please any one can tell how to create rounded button in Java like show in below figure.. enter image description here

请任何人都可以告诉如何在 Java 中创建圆形按钮,如下图所示.. 在此处输入图片说明

thanks in advance.....

提前致谢.....

回答by mre

If you're just going to use an image of a round button, then why not just use a JLabel? That is, simply invoke setIcon(...), passing your BufferedImageinstance as an argument.

如果您只想使用圆形按钮的图像,那么为什么不使用JLabel?也就是说,只需调用setIcon(...),将您的BufferedImage实例作为参数传递。

CODE

代码

public final class RoundedButtonDemo {
    private static BufferedImage bi;

    public static void main(String[] args){
        try {
            loadImage();

            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();             
                }
            });
        } catch (IOException e) {
            // handle exception
        }
    }

    private static void loadImage() throws IOException{
        bi = ImageIO.read(RoundedButtonDemo.class.getResource("../resources/login.png"));
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JLabel label = new JLabel();
        label.setIcon(new ImageIcon(bi));

        frame.getContentPane().add(label);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

OUTPUT

输出

enter image description here

在此处输入图片说明

Keep in mind that you'll need to either programmatically make the background of your image transparent, or you'll need to use an image editing tool like Paint.NET.

请记住,您需要以编程方式使图像的背景透明,或者需要使用像Paint.NET这样的图像编辑工具。

回答by Aaron Digulla

You need to write a "Look and Feel" (a theme for Java Swing). Not for the faint of heart but possible. I suggest to look at an existing theme.

您需要编写一个“外观”(Java Swing 的主题)。不适合胆小的人,但可能。我建议看一个现有的主题。

LIQUIDLNFshould be a good start.

LIQUIDLNF应该是一个好的开始。

回答by Kozio?ek

You can use JavaFX to define "Rich Graphic Components" example (rounded button with gradient): http://poligloci.blogspot.com/2009/07/beauty-and-beast-javafx-12-in-netbeans.html

您可以使用 JavaFX 来定义“丰富的图形组件”示例(带渐变的圆形按钮):http: //poligloci.blogspot.com/2009/07/beauty-and-beast-javafx-12-in-netbeans.html