רישום תוכנית
להלן רישום תוכנית עם הערות נוספות.
/*File Event16.java Copyright 1997, R.G.Baldwin This program is designed to be compiled and run under JDK 1.1 This program implements a custom scheme for moving the focus among a set of visual components using the F12 key and the Shift-F12 key as an alternate to the Tab and Shift-Tab key. The Tab and Shift-Tab key also work in the standard manner. Tab and Shift-Tab are automatically implemented by the system, and skip the Label object and the Frame object when moving the focus. This custom scheme based on the F12 key does not skip the Label object or the Frame object. For simplicity, this program does not support response to the keyboard while the component has the focus. Such response has been illustrated in earlier lessons and could be added with very little additional code. A Button object, a TextField object, and a Label object are placed in a Frame object. A FocusListener object is instantiated and registered to listen for focusGained() and focusLost() events on the Frame and the Label. When these events occur, the listener object makes a color change on the Frame or Label to provide a visual indication of focus gained or lost. When the Label has the focus, its text is red. Otherwise, its text is black. When the Frame has the focus, its background color is blue. Otherwise, it is white. Note that the Button and the TextField components automatically provide a visual indication of focus without intervention by the programmer. Also for simplicity, the ability to shift the focus to the Label and the Frame using the mouse was not included in this program. This capability has been illustrated in an earlier lesson and could be added with very little additional code required. Note that the Button and the TextField automatically receive the focus when clicked without intervention by the programmer. A KeyListener object is instantiated and registered to listen for keyPressed() events on the Frame, Label, Button, and TextField objects. This object is used to listen for the F12 or Shift-F12 key and to implement the program-specified change in focus when such a keypress occurs. Finally, a WindowListener object is instantiated and registered to terminate the program when the user closes the Frame object. This program retrieves and saves the component names that are automatically assigned to the visual components (under the assumption that the system will assign unique names) and uses those names to determine which component generated an event when such determination is necessary. No attempt was made to synchronize this focus traversal method with other capabilities to shift the focus. These results were produced using JDK 1.1, Beta 3 running under Windows 95. */ //========================================================================== import java.awt.*; import java.awt.event.*; public class Event16 { public static void main(String[] args){ GUI gui = new GUI();//instantiate a Graphical User Interface object }//end main }//end class Event16 //=========================================================================== //The following class is used to instantiate a graphical user interface object. class GUI { String myTextFieldName; //save automatic component name here String myButtonName; String myFrameName; String myLabelName; Frame myFrame;//ref variable passed as parameter Label myLabel; TextField myTextField; Button myButton; int focusIndex; public GUI(){//constructor //Create several visual components myFrame = new Frame(); myFrame.setSize(250,300); myFrame.setTitle("Copyright 1997, R.G.Baldwin"); myFrameName = myFrame.getName(); myButton = new Button("Button Object"); myButtonName = myButton.getName(); myTextField = new TextField("TextField Object"); myTextFieldName = myTextField.getName();//save the name of the component myLabel = new Label("LabelObject"); myLabelName = myLabel.getName(); myLabel.setBackground(Color.yellow);//make it yellow so it will show up //Add the other objects to the frame using default border layout manager myFrame.add("North",myButton); myFrame.add("South",myTextField); myFrame.add("West",myLabel); myFrame.setVisible(true);//make the frame visible myTextField.requestFocus();//initialize the focus to the TextField focusIndex = 0;//set focusIndex to match //Instantiate and register a FocusListener object which will process // focus events on two different visual components. In this case, // the listener object makes a color change on the visual component // to indicate focus gained or focus lost. myFocusListener focusHandler = new myFocusListener(this); myFrame.addFocusListener(focusHandler); myLabel.addFocusListener(focusHandler); //Instantiate and register a KeyListener object which will process key // events on four different visual components. This object causes the // focus to move in a program-defined manner when the user presses the // F12 key and to move in the reverse direction when the user presses // the Shift-F12 key. myKeyListener keyHandler = new myKeyListener(this); myFrame.addKeyListener(keyHandler); myTextField.addKeyListener(keyHandler); myButton.addKeyListener(keyHandler); myLabel.addKeyListener(keyHandler); //Instantiate and register a WindowListener object which will terminate // the program when the user closes the Frame object WProc1 winProcCmd1 = new WProc1(); myFrame.addWindowListener(winProcCmd1); }//end constructor }//end class GUI definition //======================================================================= //This is a low-level event listener class. // This FocusListener class makes a color change on the component to // provide a visual indication of the gain or loss of focus. class myFocusListener implements FocusListener{ GUI thisObject; myFocusListener(GUI thisObjectIn){//constructor thisObject = thisObjectIn; }// end constructor public void focusGained(FocusEvent e){ if( e.toString().indexOf("on " + thisObject.myFrameName) != -1 ){ thisObject.myFrame.setBackground(Color.blue); }//end if if( e.toString().indexOf("on " + thisObject.myLabelName) != -1 ){ thisObject.myLabel.setForeground(Color.red); }//end if thisObject.myFrame.repaint(); }//end focusGained() public void focusLost(FocusEvent e){ if( e.toString().indexOf("on " + thisObject.myFrameName) != -1 ){ thisObject.myFrame.setBackground(Color.white); }//end if if( e.toString().indexOf("on " + thisObject.myLabelName) != -1 ){ thisObject.myLabel.setForeground(Color.black); }//end if thisObject.myFrame.repaint(); }//end focusLost() }//end class myFocusListener //======================================================================= //This is a Low-Level event listener. // This listener class implements a custom scheme for moving focus // among visual components when the user presses the F12 key or the // Shift-F12 key. The F12 key moves the focus counter-clockwise on // the screen and includes the Label object and the Frame object in // the path. Shift-F12 moves the focus in the reverse direction. // The standard Tab and Shift-Tab keys work also but don't include // the Label object or the Frame object in the list of objects which // receive focus. class myKeyListener extends KeyAdapter{ GUI thisObject; myKeyListener(GUI thisObjectIn){//constructor thisObject = thisObjectIn; }//end constructor public void keyPressed(KeyEvent e){ int keyCode = e.getKeyCode(); if( (keyCode == KeyEvent.VK_F12) //if F12 and NOT Shift && (e.getModifiers() != InputEvent.SHIFT_MASK) ){ //Move the focus counter-clockwise on the screen for F12 key if(++thisObject.focusIndex > 3) thisObject.focusIndex = 0; switch(thisObject.focusIndex){ case 0: thisObject.myTextField.requestFocus(); break; case 1: thisObject.myLabel.requestFocus();break; case 2: thisObject.myButton.requestFocus();break; case 3: thisObject.myFrame.requestFocus();break; }//end switch statement }//end if statement if( (keyCode == KeyEvent.VK_F12) //if F12 AND Shift && (e.getModifiers() == InputEvent.SHIFT_MASK) ){ //Move the focus clockwise on the screen for Shift-F12 key if(--thisObject.focusIndex < 0) thisObject.focusIndex = 3; switch(thisObject.focusIndex){ case 0: thisObject.myTextField.requestFocus(); break; case 1: thisObject.myLabel.requestFocus();break; case 2: thisObject.myButton.requestFocus();break; case 3: thisObject.myFrame.requestFocus();break; }//end switch statement }//end if statement }//end keyPressed() }//end class myKeyListener //======================================================================= //The following listener is used to terminate the // program when the user closes the Frame object. class WProc1 extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); }//end windowClosing() }//end class WProc1 |