问题
I have created a button using an image:
ImageIcon water = new ImageIcon("button.jpeg");
JButton exceptionButton = new JButton(water);
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?
回答1:
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
}
来源:https://stackoverflow.com/questions/22143648/use-nimbus-to-give-rounded-corners-for-jbutton