使用Nimbus为JButton提供圆角

时间:2014-03-03 10:02:21

标签: java swing rounded-corners

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

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

  JButton  exceptionButton = new JButton(water);

我希望按钮有圆角,但我无法做到。我听说过Nimbus。我应该使用它来获得带有或不带图像的按钮的圆角?

1 个答案:

答案 0 :(得分:1)

尝试这个课程,使用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

}