» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
רישום תכנית
דף ראשי  מתקדמים  Placing Components in Containers, Absolute Coordinates  תכנית לדוגמא  רישום תכנית גרסה להדפסה

 

רישום תכנית

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

/*File Layout01.java Copyright 1997, R.G.Baldwin
Revised 10/27/97 to improve the layout on the page.

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

The program shows how to use the new methods in JDK 1.1 to 
set the size and position of components in a Frame object 
using absolute coordinates.

The program also illustrates some of the problems 
associated with the use of absolute coordinates.

A button and a yellow label are placed in a Frame object.  
Note that the two objects may overlap, and that the initial
height of the Frame object may clip off the bottom of the 
Label.

Note also that if the Frame is resized by the user, the 
Button and the Label remain fixed in size and position.  
The Frame can be resized to the point where the two 
components are no longer visible.

Note also that the specified size of the Label may not be
sufficient to display all of the initial text placed in the
label.

For simplicity, no event handler is provided for the Button
and no event handler is provided for the "Close" button on 
the Frame. Therefore, it is necessary to terminate the 
program using some other method.

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

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

//The following class is used to instantiate a graphical 
// user interface object.
class GUI {
  public GUI(){//constructor
    //Create a Button object with the specified caption and
    // the specified size and location within its container
    // (in pixels).
    Button myButton = new Button("Button");
    //Arguments are x,y,width,height
    myButton.setBounds(new Rectangle(25,50,100,75));
    
    //Create a Label object with the specified initial text
    // and the specified size and location within its 
    // container (in pixels).  Make it yellow so that its 
    // outline will be visible.    
    Label myLabel = new Label(
                            "Copyright 1997, R.G.Baldwin");
    //Arguments are x,y,width,height
    myLabel.setBounds(new Rectangle(100,100,100,75));    
    myLabel.setBackground(Color.yellow);

    //Create a Frame object with the specified title, and 
    // with no layout manager so that size and location of 
    // components shown above will be effective.
    Frame myFrame = new Frame(
                            "Copyright 1997, R.G.Baldwin");
    //Note the following null argument.
    myFrame.setLayout(null);

    //Add the two components to the Frame, set its size in
    // pixels, and make it visible.    
    myFrame.add(myButton);
    myFrame.add(myLabel);
    myFrame.setSize(250,150);
    myFrame.setVisible(true);
  }//end constructor
}//end class GUI definition
//=======================================================//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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