如何在Java ..中创建圆形的JButton? [英] How to create rounded JButton in java..?

查看:750
本文介绍了如何在Java ..中创建圆形的JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中创建舍入的JButton ...
为此,我使用了四舍五入的图像并将该图像放置在按钮上,但没有得到四舍五入的按钮.

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中创建圆形按钮,如下图所示.

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

先谢谢.....

推荐答案

如果您只想使用圆形按钮的图像,那为什么不只使用JLabel呢?也就是说,只需调用

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 BufferedImage instance as an argument.

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);
    }
}

输出

请记住,您需要以编程方式使图像的背景透明,或者需要使用图像编辑工具,例如

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.

这篇关于如何在Java ..中创建圆形的JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆