Java基础之处理事件——使窗口处理自己的事件(Skethcer 1 handing its own closing event)

控制台程序。

为表示事件的常量使用标识符可以直接启用组件对象的特定事件组。调用组件的enableEvent()方法,并把想要启用事件的标识符传送为参数,但这只在不使用监视器的情况下有效。注册监听器会自动启用监听器想要监听的事件,所以不需要调用enableEvent()方法。只有当希望对象能处理自己的某些事件时,才调用enableEvent()方法,但使用监听器可以达到相同的效果。

使用监听器对象是处理事件的首选方式,因为更容易直接启用对象的事件,代码也更简洁。

 1 // Main window for the Sketcher application
 2 import javax.swing.*;
 3 import static java.awt.event.InputEvent.*;                             // For modifier constants
 4 import java.awt.event.WindowEvent;
 5 
 6 @SuppressWarnings("serial")
 7 public class SketcherFrame extends JFrame {
 8   // Constructor
 9   public SketcherFrame(String title) {
10     setTitle(title);                                                   // Set the window title
11 
12     setJMenuBar(menuBar);                                              // Add the menu bar to the window
13 
14     JMenu fileMenu = new JMenu("File");                                // Create File menu
15     JMenu elementMenu = new JMenu("Elements");                         // Create Elements menu
16     fileMenu.setMnemonic('F');                                         // Create shortcut
17     elementMenu.setMnemonic('E');                                      // Create shortcut
18 
19     // Construct the file drop-down menu
20     newItem = fileMenu.add("New");                                     // Add New item
21     openItem = fileMenu.add("Open");                                   // Add Open item
22     closeItem = fileMenu.add("Close");                                 // Add Close item
23     fileMenu.addSeparator();                                           // Add separator
24     saveItem = fileMenu.add("Save");                                   // Add Save item
25     saveAsItem = fileMenu.add("Save As...");                           // Add Save As item
26     fileMenu.addSeparator();                                           // Add separator
27     printItem = fileMenu.add("Print");                                 // Add Print item
28 
29     // Add File menu accelerators
30     newItem.setAccelerator(KeyStroke.getKeyStroke('N',CTRL_DOWN_MASK ));
31     openItem.setAccelerator(KeyStroke.getKeyStroke('O', CTRL_DOWN_MASK));
32     saveItem.setAccelerator(KeyStroke.getKeyStroke('S', CTRL_DOWN_MASK));
33     printItem.setAccelerator(KeyStroke.getKeyStroke('P', CTRL_DOWN_MASK));
34 
35     // Construct the Element drop-down menu
36     elementMenu.add(lineItem = new JRadioButtonMenuItem("Line", true));
37     elementMenu.add(rectangleItem = new JRadioButtonMenuItem("Rectangle", false));
38     elementMenu.add(circleItem = new JRadioButtonMenuItem("Circle", false));
39     elementMenu.add(curveItem = new JRadioButtonMenuItem("Curve", false));
40     ButtonGroup types = new ButtonGroup();
41     types.add(lineItem);
42     types.add(rectangleItem);
43     types.add(circleItem);
44     types.add(curveItem);
45 
46     // Add element type accelerators
47     lineItem.setAccelerator(KeyStroke.getKeyStroke('L', CTRL_DOWN_MASK));
48     rectangleItem.setAccelerator(KeyStroke.getKeyStroke('E', CTRL_DOWN_MASK));
49     circleItem.setAccelerator(KeyStroke.getKeyStroke('I', CTRL_DOWN_MASK));
50     curveItem.setAccelerator(KeyStroke.getKeyStroke('V', CTRL_DOWN_MASK));
51 
52     elementMenu.addSeparator();
53     JMenu colorMenu = new JMenu("Color");                              // Color submenu
54     elementMenu.add(colorMenu);                                        // Add the submenu
55     colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
56     colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow", false));
57     colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
58     colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
59 
60     // Add element color accelerators
61     redItem.setAccelerator(KeyStroke.getKeyStroke('R', CTRL_DOWN_MASK));
62     yellowItem.setAccelerator(KeyStroke.getKeyStroke('Y', CTRL_DOWN_MASK));
63     greenItem.setAccelerator(KeyStroke.getKeyStroke('G', CTRL_DOWN_MASK));
64     blueItem.setAccelerator(KeyStroke.getKeyStroke('B', CTRL_DOWN_MASK));
65 
66     menuBar.add(fileMenu);                                             // Add the file menu
67     menuBar.add(elementMenu);                                          // Add the element menu
68     enableEvents(WINDOW_EVENT_MASK);                                   // Enable window events
69   }
70 
71   // Handle window events
72   protected void processWindowEvent(WindowEvent e) {
73     if (e.getID() == WindowEvent.WINDOW_CLOSING) {
74       dispose();                                                       // Release resources
75       System.exit(0);                                                  // Exit the program
76     }
77     super.processWindowEvent(e);                                       // Pass on the event
78   }
79 
80   private JMenuBar menuBar = new JMenuBar();     // Window menu bar
81 
82   // File menu items
83   private JMenuItem newItem,  openItem,   closeItem,
84                     saveItem, saveAsItem, printItem;
85 
86   // Element menu items
87   private JRadioButtonMenuItem lineItem,  rectangleItem, circleItem,  // Types
88                                curveItem, textItem;
89   private JCheckBoxMenuItem    redItem,   yellowItem,                 // Colors
90                                greenItem, blueItem ;
91 }
 1 // Sketching application
 2 import javax.swing.*;
 3 import java.awt.*;
 4 
 5 import javax.swing.SwingUtilities;
 6 
 7 public class Sketcher {
 8   public static void main(String[] args) {
 9     SwingUtilities.invokeLater(new Runnable() {
10             public void run() {
11                 createWindow();
12             }
13         });
14   }
15 
16   public static void createWindow(){
17     window = new SketcherFrame("Sketcher");                            // Create the app window
18     Toolkit theKit = window.getToolkit();                              // Get the window toolkit
19     Dimension wndSize = theKit.getScreenSize();                        // Get screen size
20 
21     // Set the position to screen center & size to half screen size
22     window.setSize(wndSize.width/2, wndSize.height/2);                 // Set window size
23     window.setLocationRelativeTo(null);                                // Center window
24     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
25 
26     window.setVisible(true);
27   }
28 
29   private static SketcherFrame window;                                 // The application window
30 }