חזרה
ש- מבלי לראות את הפתרון להלן, שדרג את התוכנית Layout01 כדי לגרום לסיומה כאשר נלחץ כפתור הclose על אובייקט המסגרת.
תוך כדי המשך השימוש בקואורדינטות מוחלטות ובשינוי גודל, שנה את התכנית כדי לוודא כי הרכיבים לא חופפים, יש שוליים של ביטחון של לפחות 25 פיקסלים בין הרכיבים והקצה של אזור הלקוח באובייקט המסגרת וכי אובייקט המסגרת הוא גדול מספיק להכיל את שני הרכיבים.
ת- ראה את התכנית הבאה.
התכנית זו מעוצבת לקומפילציה והרצה תחת 1.1JDK . כפתור ותווית צהובה ממוקמים באובייקט מסגרת. שים לב כי עם 1.1.3 JDK אם השוליים העליונים מכוונים לאפס , הכפתור מכוסה חלקית ע"י הכותרת בחלק העליון של אובייקט המסגרת. כך, הקואורדינטה המאונכת של החלק הלבן של אזור הקליינט באובייקט המסגרת לא מתחילה באפס. אלא, קואורדינטת האפס היא איפה שהוא מתחת לכותרת . נסיונות מראים כי הכותרת למעלה מכסה בערך את 20 הפיקסלים הראשונים של אזור הקליינט הלבן של אובייקט המסגרת. שים לב כי אם המשתמש משנה את גודל המסגרת, הכפתור והתווית נותרים קבועים בגודל ובעמדה. גודל המסגרת יכול להשתנות עד לנקודה בה שני הרכיבים אינם נראים יותר לעין. שים לב גם כי הגודל הנקוב של התווית עשוי שלא להיות מספיק להציג את כל הטקסט הראשוני הממוקם בתווית. לשם פשטות, לא מסופק כל מתחזק אירוע עבור הכפתור. התכנית נבחנה תוך שימוש ב 1.1.3 JDK בהרצה תחת חלונות 95
/*File SampProg134.java Copyright 1997, R.G.Baldwin Without viewing the following solution, upgrade the program named Layout01 to cause it to terminate when the close button is pressed on the Frame object. While continuing to use absolute coordinate and sizing, modify the program to ensure that the components don't overlap, there is a safety margin of at least 25 pixels between the components and the edge of the Frame object, and that the Frame object is sufficiently large to contain the two components. This program is designed to be compiled and run under JDK 1.1 A button and a yellow label are placed in a Frame object. Note that with JDK 1.1.3, if the top margin is set to zero, the button is partially covered by the banner at the top of the Frame object. Thus, the vertical coordinate of the white portion of the client area in the Frame object does not begin at zero. Rather, the zero coordinate is somewhere underneath the banner. Experimentation indicates that the banner at the top covers about the first twenty pixels of the white client area of the Frame object. Note 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. The program was tested using JDK 1.1.3 running under Win95. */ //=======================================================// import java.awt.*; import java.awt.event.*; //=======================================================// public class SampProg134 { public static void main(String[] args){ //instantiate a Graphical User Interface object GUI gui = new GUI(); }//end main }//end class SampProg134 //=======================================================// //The following class is used to instantiate a graphical // user interface object. class GUI { int leftMargin = 25;//specify the margins int topMargin = 45; int rightMargin = 25; int bottomMargin = 25; int buttonWidth = 100;//specify button width and height int buttonHeight = 75; int labelWidth = 100;//specify label width and height int labelHeight = 75; 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( leftMargin,topMargin,buttonWidth,buttonHeight)); //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. Base its location on the // location and the size of the Button object. Label myLabel = new Label( "Copyright 1997, R.G.Baldwin"); //Arguments are x,y,width,height myLabel.setBounds(new Rectangle( leftMargin + buttonWidth,topMargin + buttonHeight, labelWidth,labelHeight)); 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( leftMargin + buttonWidth + labelWidth + rightMargin, topMargin + buttonHeight + labelHeight + bottomMargin); myFrame.setVisible(true); myFrame.addWindowListener(new WindowClose()); }//end constructor }//end class GUI definition //=======================================================// //Class used to instantiate window listener objects. class WindowClose extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(1); }//end windowClosing() }//end class WindowClose |