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

 

חזרה

 

ש- מבלי לראות את הפתרון בהמשך, כתוב אפליקצית java אשר עונה  על הפרטים המובאים בתחילת התשובה.

ת – פירוט הדרישות:

כתוב אפליקציה העומדת בהוראות הבאות: התכנית מתחילה עם אובייקט מסגרת  על המסך. החצי העליון של אזור הקליינט של המסגרת הוא צהוב. החצי התחתון של אזור הקליינט של המסגרת הוא ירוק. החצי העליון מכיל 6 כפתורים . כל ששת הכפתורים הם באותו גודל , הם מאורגנים ב 2 שורות של 3 עמודות. הצהוב נראה דרך הרווח בין הכפתורים. המחצית התחתונה כוללת 2 כפתורים , אחד מתויג "2X3" והאחר מתויג "3X2". כאשר אתה מקיש על כפתור "2X3", 6 הכפתורים במחצית העליונה מאורגנים מחדש לתוך 3 שורות של 2 עמודות. כאשר אתה מקיש על כפתור ה 3X2 6 הכפתורים מאורגנים מחדש לתוך 2 שורות של 3 עמודות. מבלי להתחשב בארגון של הכפתורים בחצי העליון, אזור הקליינט של המסגרת ממשיך להיות מחולק ל 2 חצאים  שווים. המחצית התחתונה ירוקה, והעליונה צהובה. כאשר אתה סוגר את אובייקט המסגרת, התכנית מסתיימת ומחזירה שליטה למערכת ההפעלה. התכנית נבחנה תו שימוש ב 1.1.3 JDK ברצה תחת חלונות 95. להלן פירוט תוכנית התשובה

 

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

Write an application that meets the following 
specifications:

The program begins with a Frame object on the screen.

The top half of the client area of the frame is yellow.

The bottom half of the client area of the frame is green.

The top half contains six buttons. All six buttons are the
same size,

The six buttons are arranged in two rows of three columns.

The yellow shows through the gap between the buttons.

The bottom half contains two buttons, one labeled "3x2"
and the other labeled "2x3".

When you click the 3x2 button, the six buttons in the top
half are rearranged into three rows of two columns.

When you click on the 2x3 button, the six buttons are
rearranged into two rows of three columns.

Regardless of the arrangement of the buttons in the top
half, the client area of the frame continues to be divided
into two equal halves.  The bottom half is green, and
the top half is yellow.  

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

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

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

class GUI {
  Panel panel1;//The Action listener req a ref to panel1
  
  public GUI(){//constructor
    //Instantiate two button objects that will later 
    // become functional
    Button button7 = new Button("3x2");
    Button button8= new Button("2x3");    

    //Instantiate a layout manager object to be used with 
    // a Panel object.  Make the gap 3 pixels.
    //Args: row,col,Hgap,Vgap
    GridLayout myGridLayout = new GridLayout(2,3,3,3);

    //Instantiate the first of two Panel objects that will 
    // be combined onto a Frame object and make 
    // it yellow.
    panel1 = new Panel();
    panel1.setBackground(Color.yellow);
    
    //Specify the GridLayout manager for the Panel object
    panel1.setLayout(myGridLayout);
    
    //Place six Button objects on the Panel with labels 
    // as shown
    for(int cnt = 0; cnt < 6; cnt++)
      panel1.add(new Button("Button" + cnt));

    //Instantiate the second Panel object using default 
    // FlowLayout and place two Button objects on it.  
    // These buttons will become functional later when 
    // ActionListener objects are registered on them.
    Panel panel2 = new Panel();
    panel2.setBackground(Color.green);
    panel2.add(button7);
    panel2.add(button8);  

    //Instantiate a Frame object which will become the 
    // top-level user-interface object.  
    Frame myFrame = new Frame(
                            "Copyright 1997, R.G.Baldwin");
    //Note that the zero in the following argument list 
    // allows for an many rows as are needed to accommodate
    // the data.                            
    myFrame.setLayout(new GridLayout(0,1));
        
    //Add the two previously prepared Panel objects to the 
    // Frame object based on the GridLayout defined above
    // to create the composite user-interface object.
    myFrame.add(panel1);
    myFrame.add(panel2);

    myFrame.setSize(250,150);
    myFrame.setVisible(true);

    //Instantiate action listener objects and register on 
    // button7 & button8
    button7.addActionListener(
        new A3x2ActionListener(myGridLayout,myFrame,this));
    button8.addActionListener(
        new A2x3ActionListener(myGridLayout,myFrame,this));
    
    //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
//=======================================================//

//The next two classes are ActionListener classes.  One 
// object of each is instantiated and registered on the two
// active buttons respectively.  The purpose of these event
// handlers is to modify the GridLayout manager for one of 
// the Panel objects that make up the composite 
// user-interface object.  The first of these two classes 
// sets the grid to 3 rows by 2 columns.  The other class 
// sets the grid to 2 rows by 3 columns.

//=======================================================//
class A3x2ActionListener implements ActionListener{
  GridLayout myGridLayoutObject;
  Frame myFrameObject;
  GUI myGuiObject;
  
  //constructor  
  A3x2ActionListener(GridLayout layoutObject,
                           Frame inFrame,GUI inGuiObject){
    myGridLayoutObject = layoutObject;
    myFrameObject = inFrame;
    myGuiObject = inGuiObject;
  }//end constructor
  
  //When an action event occurs, set the rows to 3 and the 
  // columns to 2 in the GridLayout object.  Then set the 
  // layout manager for the frame to be the newly-modified 
  // GridLayout object.  Then validate the frame to ensure 
  // a valid layout so that the new visual will 
  // take effect.
  public void actionPerformed(ActionEvent e){
    myGridLayoutObject.setRows(3);
    myGridLayoutObject.setColumns(2);
    myGuiObject.panel1.setLayout(myGridLayoutObject);
    myFrameObject.validate();
  }//end actionPerformed()
}//end class A3x2ActionListener
//=======================================================//

class A2x3ActionListener implements ActionListener{
  GridLayout myGridLayoutObject;
  Frame myFrameObject;
  GUI myGuiObject;
  
  //constructor
  A2x3ActionListener(GridLayout layoutObject,
                            Frame inFrame,GUI inGuiObject){
    myGridLayoutObject = layoutObject;
    myFrameObject = inFrame;
    myGuiObject = inGuiObject;
  }//end constructor
  
  //When an action event occurs, set the rows to 2 and the 
  // columns to 3 in the GridLayout object.  Then set the 
  // layout manager for the frame to be the newly-modified 
  // GridLayout object.  Then validate the frame to ensure 
  // a valid layout so that the new visual will 
  // take effect.
  public void actionPerformed(ActionEvent e){
    myGridLayoutObject.setRows(2);
    myGridLayoutObject.setColumns(3);
    myGuiObject.panel1.setLayout(myGridLayoutObject);
    myFrameObject.validate();
  }//end actionPerformed()
}//end class A3x2ActionListener
//=======================================================//

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
//=======================================================//
 11-11-03 / 19:50  נוצר ע"י רונית רייכמן  בתאריך 
 רישום תכנית - הקודםהבא - CardLayout 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 2