Import java. awt .*;
Import java. awt. event .*;
Import javax. swing .*;
// Display windows of various styles
Public class LookAndFeelDemo extends JFrame {
Public LookAndFeelDemo (){
Super ("windows of various styles"); // calls the parent class Constructor
Container container = getContentPane (); // obtain the Container
JMenu menuTheme = new JMenu ("window style"); // initialize the menu
JMenuItem itemNative = new JMenuItem ("System Platform style"); // initialize the menu item
JMenuItem itemMotif = new JMenuItem ("Motif style ");
JMenuItem itemMetal = new JMenuItem ("cross-platform style ");
MenuTheme. add (itemNative); // add a menu item
MenuTheme. add (itemMotif );
MenuTheme. add (itemMetal );
ItemNative. addActionListener (new ActionListener () {// process menu item events
Public void actionreceivmed (ActionEvent event ){
ChangeLookAndFeel ("Native"); // call the method to change the window style
}
});
ItemMotif. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent event ){
ChangeLookAndFeel ("Motif ");
}
});
ItemMetal. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent event ){
ChangeLookAndFeel ("Metal ");
}
});
JMenuBar menuBar = new JMenuBar (); // initialize the menu bar
MenuBar. add (menuTheme); // add a menu to the menu bar
SetJMenuBar (menuBar); // set the menu
JPanel panel = new JPanel (); // initialize a JPanel
Panel. setBorder (BorderFactory. createTitledBorder ("component style"); // set the Boundary
Panel. add (new JTextField ("text box: Look and feel test"); // add a component to the panel.
Panel. add (new JCheckBox ("bold "));
Panel. add (new JCheckBox ("italic "));
Panel. add (new JCheckBox ("underline "));
Panel. add (new JButton ("OK "));
Panel. add (new JButton ("exit "));
Container. add (panel); // add the panel to the container.
SetSize (220,200); // set the window size
SetVisible (true); // set the window to be visible
Setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); // exit the program when the window is closed.
}
// Change the window style
Public void changeLookAndFeel (String type ){
Try {
If (type. equals ("Native") {// determines which menu item comes from
UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName (); // set the interface style
}
Else if (type. equals ("Motif ")){
UIManager. setLookAndFeel ("com. sun. java. swing. plaf. motif. MotifLookAndFeel ");
}
Else if (type. equals ("Metal") {UIManager. setLookAndFeel (
UIManager. getCrossPlatformLookAndFeelClassName ());
}
Javax. swing. SwingUtilities. updateComponentTreeUI (this); // update the interface
}
Catch (Exception ex) {// catch an error
Ex. printStackTrace (); // Output Error
}
}
Public static void main (String [] args ){
New LookAndFeelDemo ();
}
}