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

 

חזרה

 

ש- מבלי לראות את הפתרון בהמשך, כתוב אפליקצית Java אשר מתחילה עם אובייקט מסגרת על המסך עם רוחב של 280 וגובה של 100 פיקסלים. השתמש במתודת setSize() על אובייקט המסגרת להשיג את הגודל המדויק הזה. שמך חייב להופיע בכותרת הענק בחלק העליון של המסגרת. המסגרת מכילה חמישה כפתורים ריבועיים ללא כותרות. חמשת הכפתורים נמצאים בתור על פני אזור הקליינט של המסגרת עם הפרדה שווה בין הכפתורים.

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

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

כאשר אתה סוגר את המסגרת התכנית מסתיימת.

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

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

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

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

כאשר אתה סוגר את המסגרת התכנית מסתיימת.

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

 

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

The program starts with a Frame object on the screen with
a size of 280 wide by 100 pixels tall.  

Your name must appear in the banner at the top of the 
frame.

The frame contains five square buttons with no captions.
The five buttons are in a row across the client area
of the frame with equal separation between the buttons.

The buttons are separated from one another by a gap of
approximately 3 pixels.

When you click on any of the buttons, the gap between
the buttons increases by 5 pixels and the size of every 
button is decreased.

As you continue to click on the buttons, the gap continues
to increase by five pixels each click, the buttons 
continue to decrease in size, and the horizontal 
position of each buttons stay the same.  In other words,
the horizontal distance between the centers of the
buttons does not change as the size of the gap between
them increases.

Eventually, the buttons become so small that they disappear.

When you close the frame, the program terminates.

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

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

class GUI {
  public GUI(){//constructor
    Frame myFrame = new Frame(
                            "Copyright 1997, R.G.Baldwin");
    //Instantiate a FlowLayout object with CENTER 
    // alignment and a Vgap and Hgap of 3 pixels.
    FlowLayout myFlowLayout = 
                     new FlowLayout(FlowLayout.CENTER,3,3);
    //Set the layout manager for the frame to be the 
    // FlowLayout object.
    myFrame.setLayout(myFlowLayout);

    //Instantiate five MyButtonClass objects    
    MyButtonClass button1 = new MyButtonClass();
    MyButtonClass button2 = new MyButtonClass();
    MyButtonClass button3 = new MyButtonClass();
    MyButtonClass button4 = new MyButtonClass();
    MyButtonClass button5 = new MyButtonClass();

    //Add the five MyButtonClass objects to the Frame 
    // object in the order specified.
    myFrame.add(button1);
    myFrame.add(button2);
    myFrame.add(button3);
    myFrame.add(button4);
    myFrame.add(button5);

    myFrame.setSize(280,100);
    myFrame.setVisible(true);

    //Instantiate an action listener object and register 
    // it on all five buttons.    
    MyActionListener myActionListener = 
               new MyActionListener(myFlowLayout,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{
  FlowLayout myFlowLayoutObject;
  Frame myFrameObject;
  
  //constructor
  MyActionListener(FlowLayout layoutObject,Frame inFrame){
    myFlowLayoutObject = layoutObject;
    myFrameObject = inFrame;
  }//end constructor
  
  //When an action event occurs, increase the horizontal 
  // and vertical gap between components in the FlowLayout 
  // object.  Then set the layout manager for the frame to
  // be the newly-modified FlowLayout object.  Then 
  // validate the frame to ensure a valid layout so that 
  // the new visual will take effect.
  //Each time the frame is validated, the overridden
  // getPreferredSize() method is called to determine the
  // preferred size of the buttons.  This method reduces
  // the preferredSize by five pixels each time it is
  // called to offset the increase in the gap between the
  // buttons.  This, in turn, causes the buttons to remain
  // centered in the same horizontal position even though
  // the gap between them is increasing.
  
  public void actionPerformed(ActionEvent e){
    myFlowLayoutObject.setHgap(
                        myFlowLayoutObject.getHgap() + 5 );
    myFlowLayoutObject.setVgap(
                        myFlowLayoutObject.getVgap() + 5 );
    myFrameObject.setLayout(myFlowLayoutObject);
    myFrameObject.validate();
  }//end actionPerformed()
}//end class MyActionListener

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

//Extend the Button class to make it possible to override
// the getPreferredSize() method.  The overridden 
// method decreases the size of the button by five pixels
// each time it is called.
class MyButtonClass extends Button{
  int side = 55;
  
  public synchronized Dimension getPreferredSize(){
    side -= 5;
    return new Dimension(side,side);
  }//end getPreferredSize()
}//end MyButtonClass

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

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
//=======================================================//
 08-11-03 / 21:39  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תכנית - הקודםהבא - מנהל GridLayout (שרטוט רשת) 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 5