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

סקירה

ש- כתוב אפליקצית Java אשר מציגה שני אובייקטים של מסגרת על המסך. כל אובייקט ברוחב 300 פיקסלים וגובה 200 פיקסלים.

 

אובייקט אחד ממוקם בפינה השמאלית עליונה של מהסך. הפינה השמאלית עליונה של האובייקט השני בקושי נוגע בפינה הימנית תחתונה של האובייקט הראשון.

 

גרום לשמך ולשם כל אובייקט להופיע ככותרת בראש כל אובייקט. אל תיתן שמות לאובייקטים.

 

גרום לשני האובייקטים לשתף אובייקט מאזין יחיד לתגובות לאירועי עכבר.

 

בכל פעם שהעכבר נלחץ בתוך אחד האובייקטים, קואורדינטות סמן העכבר מוצגות קרוב לסמן כאשר הקואורדינטה האופקית מוצגת תחילה ואחריה הקואורדינטה האנכית. שני ערכי הקואורדינטה מופרדים על יד פסיק ורווח(X, Y).

 

בנוסף גרום לשני האובייקטים לשתף אובייקט מאזין יחיד אשר תסיים את התוכנית בכל פעם שמשתמש לוחץ על כפתור הסגירה של אחד האובייקטים.

 

וודא כי האפליקציה שלך מסתיימת ומחזירה שליטה למערכת ההפעלה כאשר משתמש לוחץ על כפתור הסגירה בפינה הימנית עליונה של האובייקט.

 

ת- ראה תוכנית להלן.

 

/*File SampProg120.java from lesson 82
Copyright 1997, R.G.Baldwin
*/

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

public class SampProg120 {
  public static void main(String[] args){
    GUI gui = new GUI();
  }//end main
}//end class SampProg120
//---------------------------------------------------------

//Subclass Frame in order to override the paint method.
class MyFrame extends Frame{
  int xCoor;
  int yCoor;

  MyFrame(){//constructor
    setTitle("Baldwin " + this.getName());
    setSize(300,200);
  }//end constructor
  
  public void paint(Graphics g){//override paint() method
    //display coordinate information on the object
    g.drawString("" + xCoor + ", " + yCoor, xCoor, yCoor);
  }//end paint()
}//end class MyFrame
//---------------------------------------------------------

class GUI {
  public GUI(){//constructor
    //Create two visual objects of type MyFrame, specify
    // their locations, and make them visible.
    MyFrame myFrame1 = new MyFrame();
    myFrame1.setLocation(new Point(0,0));
    myFrame1.setVisible(true);
    
    MyFrame myFrame2 = new MyFrame();
    myFrame2.setLocation(new Point(300,200));    
    myFrame2.setVisible(true); 
    
    //Instantiate and register Listener object which will 
    // terminate the program when the user closes either 
    // window.
    WProc1 winProcCmd1 = new WProc1();
    myFrame1.addWindowListener(winProcCmd1);
    myFrame2.addWindowListener(winProcCmd1);    
    
    //Instantiate and register Listener object which will 
    // process mouse events on either MyFrame object.
    MouseProc mouseProcCmd = 
      new MouseProc(myFrame1,myFrame2);
    myFrame1.addMouseListener(mouseProcCmd);
    myFrame2.addMouseListener(mouseProcCmd);    
  }//end constructor
}//end class GUI definition
//---------------------------------------------------------

//This listener class monitors for mouse presses and 
// displays the coordinates of the mouse pointer when the 
// mouse is pressed.  The listener object distinguishes 
// between two different visual objects on the basis of 
// their component names and displays the coordinate 
// information on the visual object which generated the 
// mouse event.

class MouseProc extends MouseAdapter{
  //save references to the objects here
  MyFrame refToFrame1,refToFrame2;
  
  MouseProc(MyFrame inFrame1,MyFrame inFrame2){//constructor
    refToFrame1 = inFrame1;//save references to the frames
    refToFrame2 = inFrame2;
  }//end constructor

  //Override the mousePressed() method to respond whenever 
  // the mouse is pressed on one of the frame objects.  
  // Distinguish between the two frame objects using the 
  // component name and display the coordinates of the 
  // mouse on the correct object.
  public void mousePressed(MouseEvent e){
     if( e.getComponent().getName().
          compareTo(refToFrame1.getName()) == 0)
    { 
      refToFrame1.xCoor = e.getX();
      refToFrame1.yCoor = e.getY();
      //display coordinates on Frame1
      refToFrame1.repaint();
    }else{ 
      refToFrame2.xCoor = e.getX();
      refToFrame2.yCoor = e.getY();
      //display coordinates on Frame2
      refToFrame2.repaint();
    }//end if-else
  }//end mousePressed()
}//end class MouseProc
//---------------------------------------------------------

//The following listener is used to terminate the program 
// when the user closes either frame object.
class WProc1 extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }//end windowClosing()
}//end class WProc1


 

ש- כתוב אפליקצית Java אשר מציגה שני אובייקטים של מסגרת על המסך. הצב אובייקט פנל אדום ברביע השמאלי העליון של אובייקט המסגרת השני.

כל אובייקט מסגרת הוא בעל רוחב 300 פיקסלים וגובה 200 פיקסלים.

אובייקט מסגרת אחד ממוקם בפינה השמאלית עליונה של המסך. הפינה השמאלית עליונה של אובייקט המסגרת השני כמעט ואינו נוגע בפינה התחתונה ימנית של אובייקט המסגרת הראשון.

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

גרום לאובייקט המסגרת הראשון ולאובייקט הפנל לשתף אובייקט מאזין יחיד על מנת להגיב לאירועי עכבר. אל תאפשר לאובייקט המסגרת השני (אשר מכיל את אובייקט הפנל) לשתף את אובייקט המאזין עבור אירועי עכבר.

בכל פעם שהעכבר נלחץ בתוך אובייקט המסגרת הראשון, או על החלק האדום של אובייקט הפנל, קואורדינטות סמן העכבר מוצגות קרוב לסמן (X, Y).

בכל פעם שהעכבר נלחץ בתוך אובייקט המסגרת השני, אך לא על אובייקט הפנל האדום, אין הצגה של ערכי הקואורדינטות.

בנוסף, גרום לשני האובייקטים של המסגרת לשתף אובייקט מאזין יחיד אשר יסיים את התוכנית בכל פעם שהמשתמש ילחץ על כפתור "סגירה" על כל אחד מאובייקטי המסגרת.

וודא כי האפליקציה מסתיימת ומחזירה שליטה למערכת ההפעלה כאשר המשתמש לוחץ על כפתור הסגירה בפינה הימנית עליונה של כל אחד מאובייקטי המסגרת.

ת- ראה תוכנית להלן.

 

/*File SampProg121.java from lesson 82
Copyright 1997, R.G.Baldwin
*/

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

public class SampProg121 {
  public static void main(String[] args){
    GUI gui = new GUI();
  }//end main
}//end class SampProg121
//---------------------------------------------------------

//Subclass Frame in order to override the paint method.
class MyFrame extends Frame{
  int xCoor;
  int yCoor;

  MyFrame(){//constructor
    setTitle("Baldwin " + this.getName());
    setSize(300,200);
  }//end constructor
  
  public void paint(Graphics g){//override paint() method
    //display coordinate information on the object
    g.drawString("" + xCoor + ", " + yCoor, xCoor, yCoor);
  }//end paint()
}//end class MyFrame
//---------------------------------------------------------

//Subclass Panel in order to override the paint method.
class MyPanel extends Panel{
  int xCoor;
  int yCoor;

  MyPanel(){//constructor
    setBounds(new Rectangle(0,0,150,100));  
    setBackground(Color.red);
  }//end constructor
  
  public void paint(Graphics g){//override paint() method
    //display coordinate information on the object
    g.drawString("" + xCoor + ", " + yCoor, xCoor, yCoor);
  }//end paint()
}//end class MyFrame
//---------------------------------------------------------

class GUI {
  public GUI(){//constructor
    //Instantiate an object of type MyPanel
    MyPanel myPanel = new MyPanel();    
  
    //Instantiate two objects of type MyFrame, specify
    // their locations, and make them visible.  Place the
    // MyPanel object in the second MyFrame object.
    MyFrame myFrame1 = new MyFrame();
    myFrame1.setLocation(new Point(0,0));
    myFrame1.setVisible(true);
    
    MyFrame myFrame2 = new MyFrame();
    myFrame2.setLayout(null);
    myFrame2.setLocation(new Point(300,200));
    myFrame2.add(myPanel);
    myFrame2.setVisible(true);
   
    //Instantiate and register a Listener object which will 
    // terminate the program when the user closes either 
    // window.
    WProc1 winProcCmd1 = new WProc1();
    myFrame1.addWindowListener(winProcCmd1);
    myFrame2.addWindowListener(winProcCmd1);    
    
    //Instantiate and register a Listener object which will 
    // process mouse events on either the MyFrame object
    // or the myPanel object.
    MouseProc mouseProcCmd = 
      new MouseProc(myFrame1,myPanel);
    myFrame1.addMouseListener(mouseProcCmd);
    myPanel.addMouseListener(mouseProcCmd);    
    
  }//end constructor
}//end class GUI definition
//---------------------------------------------------------

//This listener class monitors for mouse presses and 
// displays the coordinates of the mouse pointer when the 
// mouse is pressed.  The listener object distinguishes 
// between two different visual objects on the basis of 
// their component names and displays the coordinate 
// information on the visual object which generated the 
// mouse event.

class MouseProc extends MouseAdapter{
  //save references to the objects here
  MyFrame refToFrame1;
  MyPanel refToPanel;
  
  MouseProc(MyFrame inFrame1,MyPanel inPanel){//constructor
    refToFrame1 = inFrame1;//save references to the frames
    refToPanel = inPanel;
  }//end constructor

  //Override the mousePressed() method to respond whenever 
  // the mouse is pressed on one of the frame objects.  
  // Distinguish between the two frame objects using the 
  // component name and display the coordinates of the 
  // mouse on the correct object.
  public void mousePressed(MouseEvent e){
     if( e.getComponent().getName().
          compareTo(refToFrame1.getName()) == 0)
    { //display coordinates on the Frame object
      refToFrame1.xCoor = e.getX();
      refToFrame1.yCoor = e.getY();
      refToFrame1.repaint();
    }else{//display coordinates on the Panel object
      refToPanel.xCoor = e.getX();
      refToPanel.yCoor = e.getY();
      refToPanel.repaint();
    }//end if-else
  }//end mousePressed()
}//end class MouseProc
//---------------------------------------------------------

//The following listener is used to terminate the program 
// when the user closes either frame object.
class WProc1 extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }//end windowClosing()
}//end class WProc1

 

 

ש- שכתב את התוכנית בשם Event10 ובטל את הדרישה להעברת פרמטרים לבנאים עבור אובייקטים מאזינים לאירועים.

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

 

/*File Event10A.java Copyright 1997, R.G.Baldwin
Rewrite the program named Event10 and eliminate the 
requirement to pass parameters to the constructors for the
event Listener objects.

This program is designed to be compiled and run under 

JDK 1.1

The program illustrates the sharing of a single listener 
object among two different visual components of the same 
type.

The program detects mouse events occurring on either of two
different Frame objects.  It distinguishes between the two 
objects and displays the coordinates of a mouse click on 
whichever object experienced the mouse click.

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

Note that when the program starts, the two visual 
components overlay one another.  It is necessary to move 
one of them in order to access the other.
*/
//=======================================================//

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

public class Event10A {
  public static void main(String[] args){
    //instantiate a Graphical User Interface object
    GUI gui = new GUI();
  }//end main
}//end class Event10A
//=======================================================//

//Subclass Frame in order to override the paint method.
class MyFrame extends Frame{
  int xCoor;
  int yCoor;

  MyFrame(){//constructor
    setTitle("Copyright 1997, R.G.Baldwin");
    setSize(300,200);
  }//end constructor
  
  public void paint(Graphics g){
    //display coordinate information on the visual object
    g.drawString("" + xCoor + ", " + yCoor, xCoor, yCoor);
  }//end paint()
}//end class MyFrame
//=======================================================//

//The following class is used to instantiate a graphical 
// user interface object.
class GUI {
  public GUI(){//constructor
    //Create two visual objects of type MyFrame and make 
    // them visible.
    MyFrame myFrame1 = new MyFrame();
    myFrame1.setVisible(true);
    
    MyFrame myFrame2 = new MyFrame();
    myFrame2.setVisible(true);    
    
    //Instantiate and register Listener object which will 
    // terminate the program when the user closes either 
    // window.
    WProc1 winProcCmd1 = new WProc1();
    myFrame1.addWindowListener(winProcCmd1);
    myFrame2.addWindowListener(winProcCmd1);    
    
    //Instantiate and register Listener object which will 
    // process mouse events on either MyFrame object.
    MouseProc mouseProcCmd = new MouseProc();
    myFrame1.addMouseListener(mouseProcCmd);
    myFrame2.addMouseListener(mouseProcCmd);    
  }//end constructor
}//end class GUI definition
//=======================================================//

//This listener class monitors for mouse presses and 
// displays the coordinates of the mouse pointer when the 
// mouse is pressed.  The listener object distinguishes 
// between two different visual objects and displays the 
// coordinate information on the visual object which 
// generated the mouse event.

class MouseProc extends MouseAdapter{
  //Override the mousePressed() method to respond whenever 
  // the mouse is pressed on one of the frame objects.  
  public void mousePressed(MouseEvent e){
    //Get X and Y coordinates of mouse pointer and store in
    // the Frame object.  Distinguish between the two
    // components on the basis of the source of the event.
    // Note that the following two formulations for X and Y
    // can be used to produce the same results in this
    // situation.
    ((MyFrame)e.getComponent()).xCoor = e.getX();
    ((MyFrame)e.getSource()).yCoor = e.getY();
    
    //display coordinate information   
    e.getComponent().repaint();
  }//end mousePressed()
}//end class MouseProc
//=======================================================//

//The following listener is used to terminate the program 
// when the user closes either frame object.
class WProc1 extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.exit(0);
  }//end windowClosing()
}//end class WProc1
//=======================================================//


 

ש- שכתב תוכנית בשם Event11 ובטל את הדרישה להעברת פרמטרים לבנאים עבור אובייקטים מאזינים.

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

 

/*File Event11A.java Copyright 1997, R.G.Baldwin
Rewrite the program named Event11 and eliminate the 
requirement to pass parameters to the constructor for the
listener objects.

This program is designed to be compiled and run under 
JDK 1.1

The program illustrates the sharing of a single listener 
object between two different visual components of different
types.

The program detects mouse events occurring on either a 
visual Frame object or on a visual Window object.  It 
distinguishes between the two objects and displays a 
message indicating which object generated the event.

Clicking inside the Frame object but outside the Window 
object produces the following message:

Got mousePressed event from Frame object

Clicking inside the Window object produces the following 
message:

Got mousePressed event from Window object

Closing the Frame object produces the following message and
terminates the program:

Got windowClosing event from Frame object

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

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

public class Event11A {
  public static void main(String[] args){
    //instantiate a Graphical User Interface object
    GUI gui = new GUI();
  }//end main
}//end class Event11A
//=======================================================//

//The following class is used to instantiate a graphical 
// user interface object.
class GUI {
  public GUI(){//constructor
    //Create a visual Frame object
    Frame myFrame = new Frame();
    myFrame.setSize(200,300);
    myFrame.setTitle("Copyright 1997, R.G.Baldwin");
    myFrame.setVisible(true);
    
    //Create a visual Window object inside the Frame object
    Window myWindow = new Window(myFrame);
    myWindow.setSize(100,100);
    myWindow.setVisible(true);    
    
    //Instantiate and register a Listener object which will
    // process mouse events on either the Frame object or 
    // the Window object.
    MouseProc mouseProcCmd = new MouseProc();
    myFrame.addMouseListener(mouseProcCmd);
    myWindow.addMouseListener(mouseProcCmd);    

    //Instantiate and register a Listener object which will
    // display a message and terminate the program when the
    // user closes the Frame object
    WProc1 winProcCmd1 = new WProc1();
    myFrame.addWindowListener(winProcCmd1);
  }//end constructor
}//end class GUI definition
//=======================================================//

//This listener class monitors for mouse presses and 
// displays a message when a mousePressed() event occurs on
// either the Frame object or the Window object.  The 
// message identifies which visual object generated the 
// event. The listener object distinguishes between the two
// visual objects.

class MouseProc extends MouseAdapter{

  //Override the mousePressed() method to respond whenever
  // the mouse is pressed on one of the visual objects.  
  public void mousePressed(MouseEvent e){
    System.out.print("Got mousePressed event from ");
    if(e.getSource().toString().indexOf("Frame") >= 0)
      System.out.println("Frame object");
    else
      System.out.println("Window object");
  }//end mousePressed()
}//end class MouseProc
//=======================================================//

//The following listener is used to display a message and 
// terminate the program when the user closes the Frame 
// object.
class WProc1 extends WindowAdapter{
  public void windowClosing(WindowEvent e){
    System.out.println(
              "Got windowClosing event from Frame object");
    System.exit(0);
  }//end windowClosing()
}//end class WProc1
//=======================================================//

 06-12-03 / 19:48  עודכן ,  13-10-03 / 20:34  נוצר ע"י רונית רייכמן  בתאריך 
 שיתוף אובייקט מאזין בין רכיבים ויזואליים - הקודםהבא - רישום תוכנית לדוגמא 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 7