【问题标题】:Use Nimbus to give rounded corners for JButton使用 Nimbus 为 JButton 提供圆角
【发布时间】:2014-03-05 15:17:35
【问题描述】:

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

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

  JButton  exceptionButton = new JButton(water);

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

【问题讨论】:

标签: java swing rounded-corners


【解决方案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

}

【讨论】:

  • 我正在使用 Eclipse。我可以使用您在 Eclipse 中提供的相同代码吗?
  • 是的,你可以使用它,以后最好在你的eclipse项目中使用适当的代码部分
  • @Mehdi Boussarhane:非常感谢您的帮助。我已经接受了你的回答并赞成:) 有没有办法可以使用 nimbus 为按钮提供 3d 外观?
  • 我目前使用netbeans开发Swing应用,使用一个叫做“border”的属性会很方便,从列表中选择你想要的。那么我建议你找这个属性,它是容易。
  • 你能给我一个例子,说明我应该如何使用 nimbus 将 3d 效果应用到 jbutton 上?我试过了,但不能这样做。
猜你喜欢
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 2014-08-27
  • 2014-07-26
  • 2014-04-04
  • 2010-11-03
相关资源
最近更新 更多