使用 Nimbus 为 JButton 提供圆角 [英] Use Nimbus to give rounded corners for JButton

查看:71
本文介绍了使用 Nimbus 为 JButton 提供圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用图像创建了一个按钮:

I have created a button using an image:

  ImageIcon water = new ImageIcon("button.jpeg");

  JButton  exceptionButton = new JButton(water);

我希望按钮具有圆角,但我无法做到这一点.我听说过 Nimbus.我应该如何使用它为带或不带图像的按钮获得圆角?

I want the button to have rounded corners but I am not able to do that. I have heard about Nimbus. HOw should I use it to get rounded corners for the button with or without image?

推荐答案

试试这个类,这是使用 netbeans 的一种简单方法.

Try this class, it's an easy way using netbeans.

/*
 * NewJFrame.java
 *
 * Created on 3 mars 2014, 10:08
 */

package GUI;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author  boussarhane
 */
public class NewJFrame extends javax.swing.JFrame {
    protected UIManager.LookAndFeelInfo[] m_infos;
    String[] LAFNames;

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        m_infos = UIManager.getInstalledLookAndFeels();
        LAFNames = new String[m_infos.length];
        for (int i = 0; i < m_infos.length; i++) {
            //LAFNames[i] = m_infos[i].getName();
            if (m_infos[i].getName().equals("Nimbus")) {                
                try {

                    UIManager.setLookAndFeel(m_infos[i].getClassName());
                    SwingUtilities.updateComponentTreeUI(this);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(Acceuil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");

        jButton2.setText("jButton2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(315, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(195, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration

}

这篇关于使用 Nimbus 为 JButton 提供圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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