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

 

חזרה

 

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

כאשר אתה מתחיל את התוכנית, אובייקט מסגרת מופיע על המסך ברוחב 350 פיקסלים וגובה של 200 פיקסלים. האיזור התחתון של תחום ה client   (לקוח) באובייקט המסגרת הוא צהוב. האיזור העליון הוא אדום. בהפעלה מחרוזת הטקסט First Card (כרטיס ראשון) מופיעה בחלק האדום של אובייקט המסגרת. בהפעלה ולאורך ריצת התוכנית החלק הצהוב של אובייקט המסגרת מכיל שני כפתורים אחד מעל השני. הכפתורים מתוייגים מלמעלה למטה כך:

 - הראה כרטיס ראשון.Show First Card

Show Next Card  - הראה כרטיס הבא.

רקע צהוב נראה ברווח שבין הכפתורים. החלק הצהוב של אובייקט המסגרת והכפתורים המצורפים מספקים פנל בקרה שישמש להצגת מידע בחלק האדום של אובייקט המסגרת. החלק האדום של אובייקט המסגרת הוא איזור תצוגה שיש לו את היכולת להציג שלושה כרטיסי מידע שונים. כרטיס המידע הראשון נראה בהפעלה עם התווית First Card, כרטיס המידע האחרון יכול להיחשף עם הקשה על הכפתור "הראה כרטיס הבא" מספר פעמים בהצלחה וכרטיס זה מתויג Last Card. בין הכרטיס הראשון והאחרון ישנו כרטיס נוסף שיכול להיחשף ע"י לחיצה על כפתור הראה כרטיס הבא. התוויות על הכפתורים מציינות איזה כרטיס ייחשף כאשר תלחץ על הכפתור. הכפתור הראה כרטיס הבא עובר בסבב דרך כל שלושת הכרטיסים וחושף אותם לפי סדר. כאשר אתה חושף את הכרטיס שבין שני הכרטיסים (ראשון ואחרון), החלק האדום של אובייקט המסגרת מכיל שלושה רכיבים בעמודה אנכית: שני כפתורים ומלבן ירוק. רקע אדום קטן נשאר ברווח שבין שלושת הרכיבים. הכפתור העליון בחלק האדום של אובייקט המסגרת מתוייג Display Output (הצג פלט) והכפתור האחר מתוייג Clear Output (נקה פלט). כאשר אתה לוחץ על הכפתור "הצג פלט" המילה  "Ouch" מוצגת במלבן הירוק. כאשר אתה לוחץ על הכפתור "נקה פלט" כל טקסט מוסר מהמלבן הירוק. אל תשתמש בבנאי עם פרמטרים עבור אובייקטי המאזין של שני כפתורים אלו. כאשר אתה לוחץ על כפתור ה"סגור" באובייקט המסגרת, התוכנית מסתיימת ומחזירה את השליטה למערכת ההפעלה.

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

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

/*File SampProg138.java Copyright 1997, R.G.Baldwin
From lesson 120

Without viewing the solution that follows, write a Java
application that meets the following specifications.

When you start the program, a Frame object appears on the
screen with a width of 350 pixels and a height of 200
pixels.  

The bottom portion of the client area of the Frame object 
is yellow.  The top portion of the client area of the Frame
object is red.

At startup, the text string 

First Card

appears in the red portion of the Frame object.

At startup, and throughout the running of the program, the
yellow portion of the Frame object contains two buttons, 
one above the other. These buttons are labeled (from top to
bottom) as follows:
  
Show First Card
Show Next Card

Some yellow background shows through in the space between
the two buttons.

The yellow portion of the Frame object and the associated
buttons provide a control panel that is used to display
information in the red portion of the Frame object.  

The red portion of the Frame object is a display area
that has the ability to display three different cards of
information.  

The First Card of information shows at startup with the
label

First Card

The Last Card of information can be exposed by clicking the
button "Show Next Card" several times in succession, and 
that card is labeled

Last Card

Between the first and last cards, there is an additional 
card that can be exposed by clicking on the 
"Show Next Card" button.

The labels on the buttons indicate which card will be
exposed when you click on a button.  

The "Show Next Card" button cycles through all three cards,
exposing them in order.  

When you expose the card between the first and last cards,
the red portion of the Frame object contains three 
components in a vertical column:  two buttons and a green 
rectangle.  A small portion of the red background shows
through between the components.

The top button in the red portion of the Frame object is 
labeled:

Display Output

and the other button is labeled

Clear Output

When you click on the "Display Output" button the word 
"Ouch" is displayed in the green rectangle.

When you click on the "Clear Output" button all text is 
removed from the green rectangle.

Do not use parameterized constructors for the listener
objects for these two buttons.

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

Your name should appear in the banner at the top of the
Frame object.

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

import java.awt.*;
import java.awt.event.*;
import java.util.*;
//=======================================================//
public class SampProg138 {
  public static void main(String[] args){
    GUI gui = new GUI();
  }//end main
}//end class SampProg138
//=======================================================//

class GUI {
  
  public GUI(){//constructor

    //Build an active card.
    
    Label outputLabel = new Label("     ");
    outputLabel.setBackground(Color.green);
    Button setOutputButton = new Button("Display Output");
    Button clearOutputButton = new Button("Clear Output");
    Panel outputPanel = new Panel();
    GridLayout outputLayout = new GridLayout(3,1);
    outputLayout.setVgap(5);
    outputPanel.setLayout(outputLayout);
    outputPanel.add(setOutputButton);
    outputPanel.add(clearOutputButton);
    outputPanel.add(outputLabel);
    outputPanel.setName("Output");    
    
    //Instantiate ActionListener objects and register them
    // on the set and clear buttons. These event handlers
    // will display the output or clear the display.
    setOutputButton.addActionListener(
                       new SetOutputActionListener());
    clearOutputButton.addActionListener(
                       new ClrOutputActionListener());
    //===================================================//

    //===Build a display panel for the deck of cards===
    
    //Instantiate a layout manager object to be used with 
    // a Panel object
    CardLayout myCardLayout = new CardLayout();

    //Instantiate a display Panel object that will be 
    // composited onto a Frame object.
    Panel displayPanel = new Panel();
    
    //Specify a CardLayout manager for the Panel object
    displayPanel.setLayout(myCardLayout);
    //make the display panel visible
    displayPanel.setBackground(Color.red);

    //Add objects to the display panel using the specified
    // CardLayout manager
    displayPanel.add(new Label("First Card"),"first");    
    displayPanel.add(outputPanel,"output panel");
    displayPanel.add(new Label("Last Card"),"last");

    //======== Build the control panel ======//

    //Instantiate button objects that will be used to 
    // cycle through the cards in the deck.
    Button firstButton= new Button("Show First Card");    
    Button nextButton = new Button("Show Next Card");
 
    //Instantiate action listener objects and register on 
    // the buttons on the control panel
    firstButton.addActionListener(
             new FirstListener(myCardLayout,displayPanel));
    nextButton.addActionListener(
              new NextListener(myCardLayout,displayPanel));

    //Instantiate a control Panel object and place the 
    // Button objects on it.  
    Panel controlPanel = new Panel();
    BorderLayout controlLayout = new BorderLayout();
    controlLayout.setVgap(5);
    controlPanel.setLayout(controlLayout);
    controlPanel.add(firstButton,"North");
    controlPanel.add(nextButton,"South");
    controlPanel.setBackground(Color.yellow);

    //== Build the Top-Level User-Interface Object ==

    //Instantiate a Frame object 
    Frame myFrame = new Frame(
                            "Copyright 1997, R.G.Baldwin");
    
    //Add the display panel and the control panel objects 
    // to the Frame object to create the composite 
    // user-interface object.
    myFrame.add(displayPanel,"North");
    myFrame.add(controlPanel,"South");

    myFrame.setSize(350,200);//set the size
    myFrame.setVisible(true);//make it visible

    
    //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
//=======================================================//

//An object of this ActionListener class is registered on 
// a Button object on a Panel object.  When an event 
// occurs, this handler modifies the text in a Label object
// on the same panel.  
    class SetOutputActionListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
        //Construct an array of the components on the
        // panel that is the parent object of the source
        // of the event.  Note the requirement for casting.
        Component[] comps = ((Component)e.getSource()).
                               getParent().getComponents();

        //Identify the Label component in the array and
        // modify its text.
        for(int cnt = 0; cnt < comps.length; cnt++){
          if(comps[cnt].toString().indexOf("Label") != -1)
            ((Label)comps[cnt]).setText("Ouch");
        }//end for loop
      }//end actionPerformed()
    }//end class SetOutputActionListener    

//=======================================================//
//An object of this ActionListener class is registered on 
// a Button object on a Panel object.  When an event 
// occurs, this handler clears the text in a Label object
// on the same panel. This ActionListener is used to clear
// the names.

    class ClrOutputActionListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
        //Construct an array of the components on the
        // panel that is the parent object of the source
        // of the event.  Note the requirement for casting.
        Component[] comps = ((Component)e.getSource()).
                               getParent().getComponents();

        //Identify the Label component in the array and
        // modify its text.
        for(int cnt = 0; cnt < comps.length; cnt++){
          if(comps[cnt].toString().indexOf("Label") != -1)
            ((Label)comps[cnt]).setText("");
        }//end for loop
      }//end actionPerformed()
    }//end class ClrOutputActionListener
//=======================================================//

//Objects of the next two ActionListener classes are 
// registered on the Button objects on the control panel.
// When an event occurs, the event handler causes a card 
// in the deck of cards to be displayed on the display 
// panel.  For example, an object of the following class 
// causes the first card in the deck to be displayed.  All
// of the classes are very similar, differing only by
// one statement in the overridden ActionPerformed 
// method which specifies the action to be taken.

class FirstListener implements ActionListener{
  Panel myPanelObject;
  CardLayout myCardLayoutObject;
  
  //constructor
  FirstListener(CardLayout inCardLayout,Panel inPanel){
    myCardLayoutObject = inCardLayout;
    myPanelObject = inPanel;
  }//end constructor
  
  public void actionPerformed(ActionEvent e){
    myCardLayoutObject.first(myPanelObject);
  }//end actionPerformed()
}//end class NextListener
//=======================================================//

//See comments above in class FirstListener
class NextListener implements ActionListener{
  Panel myPanelObject;
  CardLayout myCardLayoutObject;
  
  //constructor
  NextListener(CardLayout inCardLayout,Panel inPanel){
    myCardLayoutObject = inCardLayout;
    myPanelObject = inPanel;
  }//end constructor
  
  public void actionPerformed(ActionEvent e){
    myCardLayoutObject.next(myPanelObject);
  }//end actionPerformed()
}//end class NextListener
//=======================================================//

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
//=======================================================//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 11-11-03 / 20:22  נוצר ע"י רונית רייכמן  בתאריך 
  CardLayout - הקודםהבא - תוכנית לדוגמא 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 4