Here is an example using UIManager.put("nimbusBase",Color c) for NimbusLookAndFeel illustrating eight components in swing. Here, i've used gray color, you can use any of the colors and choose from rgb or hex.
UIManager is present in javax.swing package and Object is present in java.lang package.
Structure of UIManager.put()
UIManager.put(Object propertyName, Object propertyValue);
UIManager is present in javax.swing package and Object is present in java.lang package.
What is nimbusBase?
nimbusBase is a property name that is given as the first parameter to the UIManager.put() method, this represents the base color of the components in the nimbus look and feel.
Ready for firing? OK. Let's go!
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.*;
class NimbusBaseDemo extends JFrame
{
// Declare variables, don't create objects. All are neither public nor private nor protected. They are of nomodifier.
JMenuBar mbar;
JMenu menu,submenu;
JMenuItem menuitem,submenuitem;
JButton jb;
JToggleButton jt;
JComboBox jc;
JList<String> jl;
Vector<String> v;
JSpinner jp;
JProgressBar jsp;
JSlider jsl;
// Default value for i=0, because that's Java! :)
int i;
public NimbusBaseDemo()
{
try
{
// Set nimbus look and feel. nimbusBase works only for it.
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
// Set the property nimbusBase (base color) as gray
UIManager.put("nimbusBase",Color.gray);
}catch(Exception e){}
// Set frame properties
setTitle("Nimbus Base Demo");
setSize(400,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// Create JButton with text Button
jb=new JButton("Button");
// Add JButton jb to JFrame
add(jb);
// Create JToggleButton with text ON/OFF
jt=new JToggleButton("ON/OFF");
// Remove the blue border when pressed.
jt.setFocusPainted(false);
// Add JToggleButton jt to JFrame
add(jt);
// Create JMenuBar mbar
mbar=new JMenuBar();
// Create JMenu with text Menu
menu=new JMenu("Menu");
// Create JMenuItem with text MenuItem
menuitem=new JMenuItem("MenuItem");
// Create JMenu with text Sub Menu
submenu=new JMenu("Sub Menu");
// Create JMenuItem with text Sub Menu > Menu Item
submenuitem=new JMenuItem("Sub Menu > Menu Item");
// Add menuitem to JMenu menu
menu.add(menuitem);
// Add submenuitem to submenu
submenu.add(submenuitem);
// Add submenu to menu
menu.add(submenu);
// Add menu to menubar
mbar.add(menu);
// Add menubar. Don't set.
add(mbar);
// Create JComboBox jc
jc=new JComboBox();
// Add items to JComboBox jc
jc.addItem("Apples");
jc.addItem("Mangoes");
jc.addItem("Oranges");
jc.addItem("Watermelons");
// Add JComboBox jc to JFrame
add(jc);
// Create a Vector that can hold String objects.
v=new Vector<String>();
// Add items to the Vector v
v.add("C");
v.add("C++");
v.add("Java");
v.add("Python");
v.add("Perl");
v.add("Ruby");
// Create a JList that contains items in Vector v.
jl=new JList<String>(v);
// Make the JList fat.
jl.setPreferredSize(new Dimension(200,200));
// Create a JScrollPane for JList jl
JScrollPane js=new JScrollPane(jl);
// Add JScrollPane js to JFrame
add(js);
// Create JSpinner jp
jp=new JSpinner();
// Make it fat.
jp.setPreferredSize(new Dimension(50,25));
// Add JSpinner jp to JFrame
add(jp);
// Create a JProgressBar jsp
jsp=new JProgressBar();
// Add jsp
add(jsp);
// Create a javax.swing.Timer object that executes every 20 milliseconds.
Timer t=new Timer(20,new ActionListener(){
// What it has to do for every 20 milliseconds?
public void actionPerformed(ActionEvent ae)
{
// If i equals to 100, then set it 0 (Repeat forever)
if(i==100) i=0;
// Increment i
i++;
// Set the value i to JProgressBar, the effect comes.
jsp.setValue(i);
}
});
// Start the timer
t.start();
// Create JSlider jsl
jsl=new JSlider();
// Add JSlider jsl to JFrame
add(jsl);
}
public static void main(String args[])
{
new NimbusBaseDemo();
}
}
Screenshot!
nimbusBase on Nimbus Look And Feel |
Nothing more to explain, everything is done in the program in the form of comments. So fine, take a look at other Swing Hacks.
No comments:
Post a Comment