Java: Simple Swing user interface and Listener button click event __java

Source: Internet
Author: User

The following program displays a button and a label. Each time the button is clicked, the label is updated.

Package hustspy;          
Import javax.swing.*;
Import java.awt.*;

Import java.awt.event.*;
    public class Hello implements ActionListener {private static String Labelprefix = "Number of button clicks:";
    private int numclicks = 0;

    Final JLabel label = new JLabel (Labelprefix + "0");  Specify the look and feel. Valid values://null (Use the default), "Metal", "System", "Motif", "GTK +" final static String Lookandfeel = "Syst

    Em ";
        Public Component createcomponents () {JButton button = new JButton ("I ' m a Swing button!");
        Button.setmnemonic (keyevent.vk_i);
        Button.addactionlistener (this);

        Label.setlabelfor (button);  * * An easy way to put spaces between a top-level container * and its contents are to put the contents in
         A JPanel * that has a "empty" border.
        * * JPanel pane = new JPanel (new GridLayout (0, 1));
        Pane.add (button);
        Pane.add (label);Pane.setborder (Borderfactory.createemptyborder,//top //left,//bottom 30

        )//right);
    return pane;
        public void actionperformed (ActionEvent e) {numclicks++;
    Label.settext (Labelprefix + numclicks);

        private static void Initlookandfeel () {String lookandfeel = null; if (Lookandfeel!= null) {if (Lookandfeel.equals ("Metal")) {Lookandfeel = Uimanager.getcross
            Platformlookandfeelclassname ();
            else if (lookandfeel.equals ("System")) {Lookandfeel = Uimanager.getsystemlookandfeelclassname (); else if (lookandfeel.equals ("Motif")) {Lookandfeel = "com.sun.java.swing.plaf.motif.MotifLookAnd
            Feel "; else if (lookandfeel.equals ("GTK +]) {//new in 1.4.2 Lookandfeel = "Com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
                                   else {System.err.println ("Unexpected value of Lookandfeel specified:"
                + Lookandfeel);
            Lookandfeel = Uimanager.getcrossplatformlookandfeelclassname ();
            try {uimanager.setlookandfeel (lookandfeel); 
                                   catch (ClassNotFoundException e) {System.err.println ("couldn" t find class for specified look and feel: "
                + Lookandfeel);
                System.err.println ("Did you include the L&f library in the class path?");
            System.err.println ("Using The default look and feel."); 
                                   catch (Unsupportedlookandfeelexception e) {System.err.println ("Can" T use the specified look and feel (" + Lookandfeel + ") on THis platform. ");
            System.err.println ("Using The default look and feel.");
                                   The catch (Exception e) {System.err.println ("couldn" t get specified look and feel ("
                + Lookandfeel + "), for some reason.");
                System.err.println ("Using The default look and feel.");
            E.printstacktrace ();  }}/** * Create the GUI and show it.
     For thread safety, * This method should is invoked from the * event-dispatching thread.

        * * private static void Createandshowgui () {//set the look and feel.---Set appearance, you can ignore Initlookandfeel ();
    	Make sure we have nice window decorations.

        If set to False, the appearance jframe.setdefaultlookandfeeldecorated (true) is not changed;
        Create and set up the window.
        JFrame frame = new JFrame ("Swingapplication"); Frame.setdefaultcloseoperation (jframe.exit_on_close);
        Hello app = new Hello ();
        Component contents = app.createcomponents ();

        Frame.getcontentpane (). Add (contents, borderlayout.center);
        Display the window.
        Frame.pack ();
    Frame.setvisible (TRUE); public static void Main (string[] args) {//schedule A job for the event-dispatching thread://creat
        ing and showing this application ' s GUI. Javax.swing.SwingUtilities.invokeLater (New Runnable () {public void run () {Createandshowgui ()
            ;
    }
        }); }
}


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.