Friday, 23 June 2017

Using Nimbus Look And Feel in JButton (Swing) ~ foundjava

Here is how the JButton looks in the new Nimbus Look And Feel of Java 7. The following example works only in Java 7 or may be higher versions but not on Java 6 and lower.

 
import javax.swing.*;
import java.awt.*;
class NimbusButtonDemo extends JFrame
{
JButton b1,b2;
    public NimbusButtonDemo()
    {

    // set the nimbus look and feel
    try
    {
    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
    }catch(Exception e){}


    setTitle("Nimbus Button Demonstration Swing");
    setLayout(new FlowLayout());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

   
    // Create buttons
    b1=new JButton("I am first Nimbus button!");
    b2=new JButton("I am second Nimbus button!");
   
    // add buttons to the frame
    add(b1);
    add(b2);
       
    // pack the frame
    pack();
    }
    public static void main(String args[])
    {
    new NimbusButtonDemo();
    }
}
UIManager: Present in the javax.swing package. Capable of performing look and feel related operations.
setLookAndFeel: An overloaded method that takes the class name or the Look And Feel object and the sets the corresponding look to the components. It throws ClassNotFoundExceptionInstantiationException, IllegalAccessException

No comments:

Post a Comment