» נושאי לימוד
» נושאי לימוד
יום חמישי 25 באפריל 2024
חזרה
דף ראשי  מתקדמים  Event Handling in JDK 1.1, Using Abbreviated Inner Classes  חזרה גרסה להדפסה

 

חזרה

ש- מבלי לראות את הפתרון הנתון בהמשך, כתוב אפליקצית Java העונה על הדרישות הבאות:

על התוכנית להשתמש במסגרת הבאה תוך הכנסת כל קוד נוסף הדרוש.

 

The program must be written using the following skeleton 
and inserting any additional code that may be necessary.


import java.awt.*;
import java.awt.event.*;

public class SampProg127 {
//All new code must be inserted after this comment
  
//Insert the necessary additional code here

//All new code must be inserted before this comment  
}//end class SampProg127

  

When the program starts, a Frame object containing a button 
labeled Button and a Label object containing the string 
Initial Text in Label appears on the screen.  Your name
must appear in the title at the top of the frame. When the 
user clicks the button, the text in the label changes to 
Ouch.

When the user clicks the "close" box on the Frame, the 
program terminates and control is properly returned to the
operating system.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

כאשר התוכנית מתחילה, אובייקט מסגרת המכיל כפתור שעליו תווית "כפתור" ואובייקט תווית המכיל מחרוזת "Initial Text in Label” מופיעים על המסך. שמך חייב להופיע בכותרת שבראש המסגרת. כאשר המשתמש מקיש ע הכפתור, הטקסט על התווית משתנה ל ouch . כאשר המשתמש מקיש על תיבת "close” שעל המסגרת, התוכנית מסתיימת והבקרה מוחזרת כיאות למערכת ההפעלה.

ת-

/*File SampProg127.java Copyright 1997, R.G.Baldwin
Revised 9/17/97

These results were produced using JDK 1.1.3 running under 
Windows 95.
*/
//=========================================================

import java.awt.*;
import java.awt.event.*;

public class SampProg127 {

  //-------------------------------------------------------
  static public void main(String[] args){
    SampProg127 app = new SampProg127();
    //Note the syntax in the following statement where the
    // new operator is joined to the reference to the
    // object of a class in which the GUI inner class is
    // defined in order to instantiate an object of the
    // GUI class.
    GUI gui = app.new GUI(); 
  }//end main()
      
  //-------------------------------------------------------
  //Note that the following GUI class is defined inside 
  // the SampProg127 class and thus is an inner-class of 
  // the SampProg127 class.
  class GUI extends Frame{
    //The object referenced by the following reference
    // variable is accessed directly by code in an 
    // inner-class of the GUI class.
    Label myLabel;
    
    //-----------------------------------------------------  
    public GUI(){//constructor for GUI class
      setTitle("Copyright 1997, R.G.Baldwin");

      Button myButton;
      this.add(myButton = new Button("Button"),"North");
      myButton.addActionListener(
                              new ButtonActionListener() );
                                
      this.add(myLabel = new Label(
                         "Initial Text in Label"),"South");

      //Register a Listener object for event handling on 
      // the Frame object of class GUI.
      this.addWindowListener(new Terminator());
      
      //Set frame size and make visible      
      setSize(300,100);
      setVisible(true);
    }//end GUI constructor    
    
    //-----------------------------------------------------
    //Note that the ButtonActionListener class is defined
    // inside the GUI class which is defined inside the
    // SampProg127 class.
    class ButtonActionListener implements ActionListener{
      //Implement the actionPerformed method which is
      // declared in the ActionListener interface.
      public void actionPerformed(ActionEvent e){
        //Note that because this class is defined inside
        // the GUI class which is defined inside the 
        // SampProg127 class, this method has direct
        // access to the members of both the SampProg127 
        // class and the GUI class.  Therefore, this method 
        // can directly access the reference variable named 
        // myLabel without having to access it via an 
        // object of type GUI.
        myLabel.setText("Ouch");
        
      }//end actionPerformed()
    }//end ButtonActionListener class defined inside GUI 
    
    //-----------------------------------------------------
    //Note that the Terminator class is defined inside the
    // GUI class which is defined inside the SampProg127 
    // class.
    class Terminator extends WindowAdapter{
      public void windowClosing(WindowEvent e){
        System.exit(0);
      }//end windowClosing()
    }//end class Terminator defined inside GUI class
    //-----------------------------------------------------
  }//end class GUI defined inside SampProg127 class
  //-------------------------------------------------------
}//end class SampProg127
//=========================================================

.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ש- מבלי לראות את הפתרון הנתון בהמשך, כתוב אפליקצית Java העונה על הדרישות הבאות:

על התוכנית להשתמש במסגרת הבאה תוך הכנסת כל קוד נוסף הדרוש  

 

The program must be written using the following skeleton 
and inserting any additional code that may be necessary.


import java.awt.*;
import java.awt.event.*;

public class SampProg128 extends Frame{
//All new code must be inserted after this comment
  
//Insert the necessary additional code here.

//Do not define any named classes other than the class
// named SampProg128

//All new code must be inserted before this comment  
}//end class SampProg128

כאשר התוכנית מתחילה, אובייקט מסגרת המכיל כפתור שעליו תווית "כפתור"

 ואובייקט תווית המכיל מחרוזת "Initial Text in Label” מופיעים על המסך.

 שמך חייב להופיע בכותרת שבראש המסגרת.

כאשר המשתמש מקיש ע הכפתור, הטקסט על התווית משתנה ל ouch .

 

 כאשר המשתמש מקיש על תיבת "close” שעל המסגרת,

התוכנית מסתיימת והבקרה מוחזרת כיאות למערכת ההפעלה.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ת- להלן הפתרון  

/*File SampProg128.java from lesson 94
Revised 02/21/98 to correct an earlier problem which 
caused the program to violate the specifications.

Copyright 1997, R.G.Baldwin
This program is designed to be compiled and run under 
JDK 1.1

These results were produced using JDK 1.1.3 running under 
Windows 95.
**********************************************************/

import java.awt.*;
import java.awt.event.*;

public class SampProg128 extends Frame {
  Label myLabel;  
  //-----------------------------------------------------//
  static public void main(String[] args){
    SampProg128 app = new SampProg128();
  }//end main()
  //-----------------------------------------------------//

  public SampProg128(){//constructor 
    this.setTitle("Copyright 1997, R.G.Baldwin");
    Button myButton;
    this.add(myButton = new Button("Button"),"North");
    this.add(myLabel = new Label(
                       "Initial Text in Label"),"South");
    //---------------------------------------------------//
    
    //The code which follows instantiates two 
    // anonymous objects of types ActionListener and 
    // WindowAdapter, and registers them for handling 
    // events on the corresponding Button object and
    // the Frame object.  This code uses the abbreviated 
    // syntax which defines the listener classes
    // anonymously (the listener classes do not have
    // class names and the objects instantiated from
    // those classes do not have names).
    
    //Begin statement -----------------------------------
    myButton.addActionListener(
      //The following object is passed as a parameter
      // to the addActionListener() method.
      new //instantiate anonymous object of the class
        ActionListener(){//anonymous class definition
          //Implement the actionPerformed() method 
          // which is declared in the ActionListener
          // interface.
          public void actionPerformed(ActionEvent e){
            //The methods in this inner-class have direct
            // access to the members of the enclosing
            // outer-classes named SampProg128.  
            // Thus, direct access to the reference
            // variable named myLabel
            // is possible
            myLabel.setText("Ouch");
          }//end actionPerformed()
        }//end ActionListener class definition
      );//end addActionListener() statement
    //End statement -------------------------------------
       
    //Begin statement -----------------------------------
    this.addWindowListener(
    //See above discussion for explanation of this code
         new WindowAdapter(){//anonymous class definition
           public void windowClosing(WindowEvent e){
             System.exit(0);//terminate the program
           }//end windowClosing()
         }//end WindowAdapter
       );//end addWindowListener
    //End statement -------------------------------------

    //---------------------------------------------------
    //Set frame size and make it visible.
    this.setSize(300,100);
    this.setVisible(true);
  }//end SampProg128 constructor
}//end class SampProg128
//=========================================================

  

 

 

 

 

 

 

 18-10-03 / 00:56  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תכנית לדוגמא שניה - הקודםהבא - Container Events and More on Inner Classes 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 5