» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
פירוט תוכנית
דף ראשי  מתקדמים  חבילת Swing, תצוגה מקדימה של Pluggable Look and Feel  פנלPluggable Look and Feel  פירוט תוכנית גרסה להדפסה

 

פנל Pluggable Look and Feel

-פירוט תוכנית

סעיף זה מכיל פירוט מלא של המחלקה המשמשת להצגת ישומי L&F שונים.

  

/*File PlafPanel01 Copyright 1998, R.G.Baldwin
  The purpose of this class is to construct an object that
  can easily be associated with a GUI to test the GUI
  for all of the Look and Feel implementations installed 
  with the current JDK.
  
  To associate this object with a GUI under test, pass
  a reference to the JFrame containing the GUI as a
  parameter when this object is constructed. Typically,
  you will instantiate an object of this class and add
  it to a JFrame object that is separate from the GUI
  under test.
  
  This class creates a JPanel.  The JPanel contains one 
  JButton for each L&F implementation in the currently 
  installed JDK.  Clicking each JButton will cause the L&F
  of the GUI to change to the L&F represented by that 
  JButton.

  The name of the L&F is displayed on the JButton.
  
  This class will be used with a variety of programs in
  this tutorial in order to test them against the currently
  installed look and feel implementations.
  
  The key statements in selecting and then implementing
  the new L&F are:
  
  UIManager.setLookAndFeel(plafClassName);  
  
  SwingUtilities.updateComponentTreeUI(thisPlafPanel);
  SwingUtilities.updateComponentTreeUI(testGui);
                           
  These statements are discussed further in the comments
  in the program.
                           
  This class was tested using JDK 1.1.3 and Swing 1.0.1 
  under Win95.
**********************************************************/

import java.awt.event.*;
import com.sun.java.swing.*;
import java.util.*;

public class PlafPanel01 extends JPanel {
  /*
  The following is an unusual reference variable type that
  is used to refer to an array of L&F information.  As of
  Swing 1.0.1, the document that should explain this type
  is missing from the download documentation file.
  
  The best available information seems to be the following
  statement that was extracted from the description of the
  method named getInstalledLookAndFeels() that returns an 
  array object of this type (a minor typo was corrected
  by the author in this quotation):

  "Return an array of objects that provide some 
  information about the LookAndFeel implementations
  that have been installed with this java development kit.
  The LookAndFeelInfo objects can be used by an 
  application to construct a menu of look and feel options
  for the user or to set the look and feel at start up
  time."
  */
  UIManager.LookAndFeelInfo[] plafInfoArray;
  //-----------------------------------------------------//
  JFrame testGui;//save a reference to the test GUI here
  PlafPanel01 thisPlafPanel = this;//ref to this object
  
  public PlafPanel01(JFrame testGui) {//constructor
    this.testGui = testGui;//save ref to test GUI
    this.add(new JLabel(//label the PlafPanel
       "PL&F Selection Panel, Copyright 1998, RGBaldwin"));
  
    //Get the list of L&Fs installed with the current JDK
    //See note above regarding this method.
    plafInfoArray = UIManager.getInstalledLookAndFeels();
 
    //Create a vector of references to JButton objects 
    // with one element in the vector for each L&F
    // implementation in the current JDK. 
    Vector theButtons = new Vector(plafInfoArray.length);
    
    //Create one JButton object for each L&F implementation
    // and put its reference in the Vector.
    for(int cnt = 0; cnt < plafInfoArray.length; cnt++){
      theButtons.addElement(new JButton());
      
      //Get the name of the class for each specific L&F
      // implementation
      String theClassName = 
                         plafInfoArray[cnt].getClassName();
      
      //Extract a short name for each specific L&F 
      // implementation and use it to label the JButton 
      // corresponding to that L&F. The short name appears
      // following the last period in the String
      // representation of the class name for the 
      // L&F implementation. Note the requirement to
      // downcast the reference extracted from the Vector.
      String label = theClassName.substring(
                          theClassName.lastIndexOf(".")+1);
      ((JButton)theButtons.elementAt(cnt)).setText(label);
      
      //Add an action listener to each JButton that will
      // cause the L&F to change to the one represented by
      // that JButton whenever the JButton is clicked. Note
      // that because the references to the buttons are
      // stored in a Vector, it is necessary to downcast
      // them from Object to JButton.
      ((JButton)theButtons.elementAt(cnt)).
          addActionListener(new MyActionListener(
                                            theClassName));
      
      //Add each JButton to the JPanel.
      add((JButton)theButtons.elementAt(cnt));
    }//end for loop
  }//end constructor
  //=====================================================//
  
  //Inner class for action listeners
  class MyActionListener implements ActionListener{
    String plafClassName;//save name of plaf class here

    //Constructor
    MyActionListener(String plafClassName){
      //save the incoming parameter
      this.plafClassName = plafClassName;
    }//end constructor
    
    public void actionPerformed(ActionEvent e){
      //Set the current default L&F to that passed in as
      // a parameter
      try{
        UIManager.setLookAndFeel(plafClassName);
      }catch(Exception ex){System.out.println(ex);}
      
      //Now implement the current default L&F to make it
      // take effect. The description of the 
      // updateComponentTreeUI() method as extracted from
      // the documentation is as follows:
        
      //"A simple minded look and feel change: ask each 
      // node in the tree to updateUI(), i.e. to 
      // initialize its UI property with the current look
      // and feel."
      
      //Set the L&F for this PlafPanel01 object
      SwingUtilities.updateComponentTreeUI(thisPlafPanel);
      //Set the L&F for the test GUI object
      SwingUtilities.updateComponentTreeUI(testGui);
                                    
    }//end actionPerformed()
  }//end class MyActionListener
}//end class PlafPanel01
//=======================================================//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

המטרה העיקרית של תוכנית זו היא להדגים סגנון תצוגה ותחושה ניתן להתקנה (PL&F) ואת היכולת לשנות L&F  בזמן הריצה. היא גם מדגימה מספר דרישות הכרוכות בשימוש ברכיבי Swing בשונה מהשימוש ברכיבי AWT.התוכנית יוצרתGUI  המורכב מאובייקט PlafPanel101 בתוך JFrame המשמש לשליטה על ה- L&F של התוכנית. לאובייקט ה- PlafPanel101 יוצרים מופע מהמחלקה המותאמת אישית הנקראת PlafPanel101 והוא מכיל JButton אחד עבור כל אחד מיישומי ה- L&F ב- JDK המותקנת הנוכחית.

השם של ה- L&F  מוצג על כל JButton ב- PlafPanel. ה- PlafPanel101 מתווסף למיכל הקצה (content pane) של JFrame. לחיצה על אחד מה- JButton ב- PlafPanel101 תגרום ל- L&F של ה- GUI  ול- L&F של ה- PlafPanel101 להשתנות ל- L&F שמייצג ה- JButton. התוכנית יוצרת גם GUI נוסף ב- JFrame שנועד לעשות סימולצייה לGUI הנמצא בבדיקה. GUI  זה מכיל JButton לא פעיל ו- JTextField לא פעיל. תפקידם של רכיבים אלה הוא לאפשר לך לראות כיצד הם מתנהגים כאשר ה- L&F משתנה. תוכנית זו נבדקה באמצעות JDK 1.1.3 ו- Swing 1.0.1 תחת Windows 95.

אם תקמפל ותריץ את התוכנית הזו, אתה אמור לראות שני אובייקטי JFrame במסך שלך. הראשון מצד שמאל למעלה הוא ה- GUI  הנבדק, ומכיל אובייקט JButton לא פעיל ואובייקט JTextField לא פעיל. הם הוצבו שם כדי שתוכל לראות כיצד הם נראים ומתנהגים כאשר אתה מחליף בין יישומי ה- L&F הזמינים במערכת שלך. אובייקט ה- JFrame  שנמצא מתחתיו, אמור להכיל אובייקטJButton  אחד עבור כל אחד מיישומי ה- L&F  הקיימים ב- JDK הנוכחית המותקנת אצלך. (אם המערכת שלך כוללת מספר גדול של יישומי L&F, ייתכן שלא יהיה ניתן לראות חלק מהלחצנים ויהיה עליך להרחיב את ה- JFrame  באופן ידני). כל אחד מהלחצנים האלה אמור לקבל תווית עם שם ה- L&F שאותו הוא מפעיל. לחיצה על לחצנים אלה תגרום ל- L&F של שני אובייקטי ה- JFrame  להפוך ל- L&F המסוים שהלחצן מציין.

 

 05-11-03 / 19:58  נוצר ע"י רונית רייכמן  בתאריך 
 פנלPluggable Look and Feel - הקודםהבא - תוכנית בדיקה - מקטעי קוד מעניינים 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 3