Java displays window program code in multiple styles

Source: Internet
Author: User

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 ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.