» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
חזרה
דף ראשי  מתקדמים  A First Look, Delegation Event Model  חזרה גרסה להדפסה

 

חזרה

 

ש- מבלי לראות את הפתרון שמופיע אחר כך, שכתב את התכנית המכונה Layout03 עם השינויים הבאים:

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

כשהתכנית מתחילה, הכפתורים בעמדות מזרח ומערב כל אחד ברוחב של בערך  100 פיקסלים והכפתור במרכז הוא ברוחב של 50 פיקסלים בערך.

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

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

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

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

 ת – הקדמה ולאחריה פתרון לשאלה.

מבלי לראות את הפתרון שמופיע אחר כך, שכתב את התכנית המכונה Layout03 עם השינויים הבאים:

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

כשהתכנית מתחילה, הכפתורים בעמדות מזרח ומערב כל אחד ברוחב של בערך  100 פיקסלים והכפתור במרכז הוא ברוחב של 50 פיקסלים בערך.

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

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

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

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

התכנית נבחנה תוך שימוש ב1.1.3 JDK בהרצה תחת חלונות 95.

פירוט התשובה .

 

 

/*File SampProg135.java Copyright 1997, R.G.Baldwin

Without viewing the solution that follows, rewrite the
program named Layout03 with the following changes:
  
As in the program named Layout03, five buttons are added to
a Frame object using a BorderLayout object as the layout 
manager with a one-pixel gap between components in both the
horizontal and vertical direction.  
  
There are no captions on any of the buttons.

When the program starts, the buttons in the East and West
positions are each approximately 100 pixels wide and the
button in the Center is approximately 50 pixels wide.

Each time you click on any of the buttons, the horizontal
gap between buttons increases by five pixels.  However, 
the widths of the East and West buttons don't change 
appreciably.  In other words, the increase in horizontal
gap is accommodated almost entirely as a result of the
Center button becoming narrower.

If you continue to click the buttons long enough, the
Center button will shrink entirely out of sight but the
other four buttons will continue to be visible with no
apparent change in size.  The Center button can be made to
reappear by manually enlarging the Frame.

If you manually increase the width of the Frame, the
widths of the East and West buttons don't change 
appreciably.  Rather, the Center button increases in width
to accommodate the change in the width of the Frame 
object.

If you start the program running and then manually 
decrease the width of the Frame object, the Center button
decreases in width to accommodate the reduction in 
overall width.  Eventually the Center button will shrink
completely out of sight and then the East and West buttons
will collide and begin to change width.
  
A windowClosing() event listener object is instantiated and
registered on the frame to terminate the program when the 
frame is closed.

The program was tested using JDK 1.1.3 running under Win95.
*/
//=======================================================//

import java.awt.*;
import java.awt.event.*;
//=======================================================//
public class SampProg135 {
  public static void main(String[] args){
    //instantiate a Graphical User Interface object
    GUI gui = new GUI();
  }//end main
}//end class SampProg135
//=======================================================//

class GUI {
  public GUI(){//constructor
    Frame myFrame = new Frame(
                            "Copyright 1997, R.G.Baldwin");
    //Instantiate a BorderLayout object with a Vgap 
    // and Hgap of 1 pixel.
    BorderLayout myBorderLayout = new BorderLayout(1,1);
    //Set the layout manager for the frame to be the 
    // BorderLayout object.
    myFrame.setLayout(myBorderLayout);

    //Instantiate five button-like objects.  Note that two
    // of these objects are of type MyButton instead of
    // type Button.    
    Button button1 = new Button();
    MyButton button2 = new MyButton();
    Button button3 = new Button();
    MyButton button4 = new MyButton();
    Button button5 = new Button();

    //Add the five button-like objects to the Frame object
    // in the positions specified.
    myFrame.add(button1,"South");
    myFrame.add(button2,"West");
    myFrame.add(button3,"North");
    myFrame.add(button4,"East");
    myFrame.add(button5,"Center");

    myFrame.setSize(250,150);//set size of frame
    myFrame.setVisible(true);

    //Instantiate an action listener object and register 
    // it on all five buttons.    
    MyActionListener myActionListener = 
        new MyActionListener(myBorderLayout,myFrame);
    button1.addActionListener(myActionListener);
    button2.addActionListener(myActionListener);
    button3.addActionListener(myActionListener);
    button4.addActionListener(myActionListener);
    button5.addActionListener(myActionListener);
    
    //Instantiate and register a window listener to 
    // terminate the program when the Frame is closed.    
    myFrame.addWindowListener(new Terminate());
  }//end constructor
}//end class GUI definition
//=======================================================//

class MyActionListener implements ActionListener{
  BorderLayout myBorderLayoutObject;
  Frame myFrameObject;
  
  //constructor
  MyActionListener(
                  BorderLayout layoutObject,Frame inFrame){
    myBorderLayoutObject = layoutObject;//save references
    myFrameObject = inFrame;
  }//end constructor
  
  //When an action event occurs, increase the horizontal 
  // gap between components in the BorderLayout object.  
  // Then set the layout manager for the frame to be the 
  // newly-modified BorderLayout object.  Then validate 
  // the frame to ensure a valid layout so that the new 
  // visual will take effect.
  public void actionPerformed(ActionEvent e){
    myBorderLayoutObject.setHgap(
                      myBorderLayoutObject.getHgap() + 5 );
    myFrameObject.setLayout(myBorderLayoutObject);
    myFrameObject.validate();
  }//end actionPerformed()
}//end class 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
//=======================================================//

//This class extends the standard Button class in order
// to make it possible to override the getPreferredSize()
// method.  The BorderLayout manager honors the width of
// the preferred size insofar as possible to maintain the
// specified preferred width.  
class MyButton extends Button{
  
  public synchronized Dimension getPreferredSize(){
     return new Dimension(100,100);
  }//end getPreferredSize()     
  
}//end class MyButton
 08-11-03 / 21:22  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תכנית - הקודםהבא - FlowLayout 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 5