» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
חזרה
דף ראשי  מתקדמים   טיפול בארועים במחלקות מורחבות ללא אובייקטי מאזין  חזרה גרסה להדפסה

 

 

חזרה

 

ש- כתוב אפליקצית Java אשר מתאימה לפירוט הבא.

 

/*File SampProg131.java Copyright 1997, R.G.Baldwin
This program is designed to be compiled and run under 
JDK 1.1

Without viewing the solution that follows, write an 
application that contains an object that will accept text 
input, a Button object, and a Label object in a standard 
frame.  For purposes of the remainder of this 
specification, the first object listed above will be 
referred to as the Text Object.

If you enter lower case letters into the Text Object, they 
are automatically converted to upper case before being 
displayed.  

When you click on the Button, the data inside the Text
Object are copied into the Label object.

When you click on the close button on the Frame, the 
program terminates and returns control to the operating
system.

The program was tested using JDK 1.1.3 running under Win95.
*/
//=========================================================
import java.awt.*;
import java.awt.event.*;

//=========================================================

public class SampProg131 extends Frame{
  public static void main(String[] args){
    new SampProg131();//instantiate an object of this type
  }//end main
//---------------------------------------------------------
  public SampProg131(){//constructor
    this.setTitle("Copyright 1997, R.G.Baldwin");
    this.setLayout(new FlowLayout());
    this.setSize(250,100);
    
    Button myButton;
    add(myButton = new Button("Copy to Label"));

    NumericTextField myCustomTextField;
    //add a custom component
    add(myCustomTextField = new NumericTextField());

    Label myLabel;
    add(myLabel = new Label("Initial Text"));

    this.setVisible(true);

    myButton.addActionListener(
         new MyActionListener(myLabel,myCustomTextField));    
    this.addWindowListener(new Terminate()); 
      
  }//end constructor

}//end class SampProg131
//=========================================================

//Class to define a new type of TextField.  This is a 
// custom TextField component, extended from TextField.  
// It will only accept numeric values.  Note that it 
// depends on key events for its operation but it does not 
// use the source/listener mode of the Delegation Event 
// Model.
class NumericTextField extends TextField{

  NumericTextField(){//constructor
    this.setColumns(10);//set the size
    //Enable key events so that the processKeyEvent() 
    // method will be invoked whenever a key event occurs 
    // on an object of this class.
    enableEvents(AWTEvent.KEY_EVENT_MASK);
  }//end constructor
  //-------------------------------------------------------
  

  //Because key events are enabled, this overridden method 
  // will automatically be invoked whenever a key event 
  // occurs on an object of the class.
  protected void processKeyEvent(KeyEvent e){
    //KEY_TYPED is key-down and key-up
    if(e.getID() == KeyEvent.KEY_TYPED)
      //If the char is lower case, convert it to upper
      // case.
      if(e.getKeyChar()  'Z')
        e.setKeyChar((char)(e.getKeyChar() -('a' - 'A')));
    //always do this when overriding processXxEvent            
    super.processKeyEvent(e);
  }//end processKeyEvent
}//end class NumericTextField
//=========================================================

//Class to create an ActionListener for the Button object.
// Transfers the data from the NumericTextField to the 
// Label.
class MyActionListener implements ActionListener{
  Label myLabel;
  NumericTextField myCustomTextField;
  //-------------------------------------------------------
  
  MyActionListener( //constructor
                Label inLbl, NumericTextField inNumTxtFld){
    myLabel = inLbl;
    myCustomTextField = inNumTxtFld;
  }//end constructor
  //-------------------------------------------------------
  
  public void actionPerformed(ActionEvent e){
    myLabel.setText(myCustomTextField.getText());  
  }//end actionPerformed()
}//end MyActionListener
//=========================================================

class Terminate extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    //terminate the program when the window is closed  
    System.exit(0);
  }//end windowClosing
}//end class Terminate
//=========================================================
 31-10-03 / 14:54  נוצר ע"י רונית רייכמן  בתאריך 
 רישום התוכנית - הקודםהבא - הצגת מחלקות ה TOOLKIT, PEERS, והרכיבים הקלים 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 1