Java:简单的Swing用户界面和监听按钮点击事件


下面的程序,显示一个按钮和一个标签。每次点击按钮时,标签都将更新。

  1. package hustspy;  
  2.   
  3. import javax.swing.*;            
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6.   
  7. public class hello implements ActionListener {  
  8.     private static String labelPrefix = "Number of button clicks: ";  
  9.     private int numClicks = 0;  
  10.     final JLabel label = new JLabel(labelPrefix + "0    ");  
  11.   
  12.     //Specify the look and feel to use.  Valid values:   
  13.     //null (use the default), "Metal", "System", "Motif", "GTK+"   
  14.     final static String LOOKANDFEEL = "System";  
  15.   
  16.     public Component createComponents() {  
  17.         JButton button = new JButton("I'm a Swing button!");  
  18.         button.setMnemonic(KeyEvent.VK_I);  
  19.         button.addActionListener(this);  
  20.         label.setLabelFor(button);  
  21.   
  22.         /* 
  23.          * An easy way to put space between a top-level container 
  24.          * and its contents is to put the contents in a JPanel 
  25.          * that has an "empty" border. 
  26.          */  
  27.         JPanel pane = new JPanel(new GridLayout(01));  
  28.         pane.add(button);  
  29.         pane.add(label);  
  30.         pane.setBorder(BorderFactory.createEmptyBorder(  
  31.                                         30//top   
  32.                                         30//left   
  33.                                         10//bottom   
  34.                                         30//right   
  35.                                         );  
  36.   
  37.         return pane;  
  38.     }  
  39.   
  40.     public void actionPerformed(ActionEvent e) {  
  41.         numClicks++;  
  42.         label.setText(labelPrefix + numClicks);  
  43.     }  
  44.   
  45.     private static void initLookAndFeel() {  
  46.         String lookAndFeel = null;  
  47.   
  48.         if (LOOKANDFEEL != null) {  
  49.             if (LOOKANDFEEL.equals("Metal")) {  
  50.                 lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();  
  51.             } else if (LOOKANDFEEL.equals("System")) {  
  52.                 lookAndFeel = UIManager.getSystemLookAndFeelClassName();  
  53.             } else if (LOOKANDFEEL.equals("Motif")) {  
  54.                 lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";  
  55.             } else if (LOOKANDFEEL.equals("GTK+")) { //new in 1.4.2   
  56.                 lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";  
  57.             } else {  
  58.                 System.err.println("Unexpected value of LOOKANDFEEL specified: "  
  59.                                    + LOOKANDFEEL);  
  60.                 lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();  
  61.             }  
  62.   
  63.             try {  
  64.                 UIManager.setLookAndFeel(lookAndFeel);  
  65.             } catch (ClassNotFoundException e) {  
  66.                 System.err.println("Couldn't find class for specified look and feel:"  
  67.                                    + lookAndFeel);  
  68.                 System.err.println("Did you include the L&F library in the class path?");  
  69.                 System.err.println("Using the default look and feel.");  
  70.             } catch (UnsupportedLookAndFeelException e) {  
  71.                 System.err.println("Can't use the specified look and feel ("  
  72.                                    + lookAndFeel  
  73.                                    + ") on this platform.");  
  74.                 System.err.println("Using the default look and feel.");  
  75.             } catch (Exception e) {  
  76.                 System.err.println("Couldn't get specified look and feel ("  
  77.                                    + lookAndFeel  
  78.                                    + "), for some reason.");  
  79.                 System.err.println("Using the default look and feel.");  
  80.                 e.printStackTrace();  
  81.             }  
  82.         }  
  83.     }  
  84.   
  85.     /** 
  86.      * Create the GUI and show it.  For thread safety, 
  87.      * this method should be invoked from the 
  88.      * event-dispatching thread. 
  89.      */  
  90.     private static void createAndShowGUI() {  
  91.         //Set the look and feel.---设置外观,可以忽略   
  92.         initLookAndFeel();  
  93.   
  94.         //Make sure we have nice window decorations.   
  95.         //设置为false的话,即为不改变外观   
  96.         JFrame.setDefaultLookAndFeelDecorated(true);  
  97.   
  98.         //Create and set up the window.   
  99.         JFrame frame = new JFrame("SwingApplication");  
  100.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  101.   
  102.         hello app = new hello();  
  103.         Component contents = app.createComponents();  
  104.         frame.getContentPane().add(contents, BorderLayout.CENTER);  
  105.   
  106.         //Display the window.   
  107.         frame.pack();  
  108.         frame.setVisible(true);  
  109.     }  
  110.   
  111.     public static void main(String[] args) {  
  112.         //Schedule a job for the event-dispatching thread:   
  113.         //creating and showing this application's GUI.   
  114.         javax.swing.SwingUtilities.invokeLater(new Runnable() {  
  115.             public void run() {  
  116.                 createAndShowGUI();  
  117.             }  
  118.         });  
  119.     }  
  120. }  

相关内容