» נושאי לימוד
» נושאי לימוד
יום שני 29 באפריל 2024
פירוט התוכנית למתודה printAll()
דף ראשי  מתקדמים  הדפסה עם AWT and Swing  פירוט התוכנית למתודה printAll() גרסה להדפסה

 

פירוט התוכנית למתודה printAll()

סעיף זה כולל פירוט מלא של התוכנית. השיעור פותח מיד לאחר סעיף זה בדיון לגבי תוכנית הדפסה שצובעת חומר על הנייר עם AWT.

 

/*File Print04.java
Copyright 1998 R.G.Baldwin

The purpose of this program is to demonstrate the ability
to print the components in an AWT container that
is either a top-level container, or is embedded inside 
another AWT container.

Even though this application only works with AWT 
components, it is hoped that it can be made to work with
Swing components once the bugs are all worked out of Swing
and the JDK.

This program places one of two selectable Panels and four
Buttons in a Frame.

One of the buttons has a listener that causes the currently
selected Panel and all the components contained in the 
Panel to be printed. (See comment below regarding the 
printing of the Panel.)

Another of the buttons has a listener that causes the
top-level Frame container and all the components contained
in the Frame to be printed. 

Actually, with JDK 1.1.6 under Win95, the Frame itself
isn't printed.  Only its contents are printed.  This seems
to contradict the specifications for the printAll()
method in the JavaSoft documentation which contains the
following description of the printAll() method:
  
"Prints this component and all of its subcomponents."  

The same is probably true for the Panel, but a Panel
doesn't have any distinguishing characteristics that would
make it apparent that it is or isn't being printed.
  
Both of the above mentioned buttons actually share
the same listener object, but the end result is as 
described above.

The other two buttons are used to select between two 
different Panels.  In other words, the user can select
between two different panels and cause the one currently
installed in the Frame to be printed.

When the selected Panel is printed, the other components in
the Frame are ignored.

When the Frame is printed, all of the components in the
Frame, including the currently installed Panel, are 
printed.

Simply for illustration, one of the selectable Panels 
contains a Label, a TextField, and a Button that is not 
active.

The other selectable Panel contains a Label, a TextField 
and two Buttons that are not active.

Tested using JDK 1.1.6 under Win95
**********************************************************/

import java.awt.*;
import java.awt.event.*;
//=======================================================//

public class Print04 {
  public static void main(String[] args){
    //instantiate a Graphical User Interface object
    GUI gui = new GUI();
  }//end main
}//end class Print04
//=======================================================//

//This class is used to instantiate a Graphical User
// Interface object
class GUI{
  //The container named myFrame and all of the
  // components that it contains will be printed or sent
  // to a print file when the Button with the caption
  // "Print the Frame" is clicked. (With JDK 1.1.6, only
  // the components that it contains are printed. Don't
  // know if this is a bug or an interpretation issue
  // regarding the specifications for the method named
  // printAll().) 
  Frame myFrame = new Frame("Copyright 1997, R.G.Baldwin");
  
  //The container named panelToPrint and all of the
  // components that it contains will be printed or sent
  // to a print file when the Button with the caption
  // "Print the Panel" is clicked. (With JDK 1.1.6, it is
  // probable that only the components that it contains are
  // printed but this is not obvious because a Panel has
  // no distinguishing features that would cause the 
  // viewer to be able to determine if the Panel is printed
  // or not.) 
  Panel panelToPrint = null;
  
  //References to two selectable panels
  Panel panel0;
  Panel panel1;
    
  public GUI(){//constructor
    //The following button causes the panelToPrint
    // container to be printed.
    Button printPanelButton = new Button("Print the Panel");
    printPanelButton.addActionListener(
                                new PrintActionListener());
    myFrame.add(printPanelButton,"North");
    
    Button printFrameButton = new Button("Print the Frame");
    printFrameButton.addActionListener(
                                new PrintActionListener());
    myFrame.add(printFrameButton,"South");
    
    //The following buttons are used to select between two
    // different panels for display
    Button selectPanel0Button = 
                              new Button("Select Panel 0");
    selectPanel0Button.addActionListener(
                               new SelectPanel0Listener());
    myFrame.add(selectPanel0Button,"West");
    
    Button selectPanel1Button = 
                              new Button("Select Panel 1");
    selectPanel1Button.addActionListener(
                               new SelectPanel1Listener());
    myFrame.add(selectPanel1Button,"East");

    //Construct the selectable Panels that will be assigned
    // to the panelToPrint reference when a selection is
    // made.  The print routine causes the container 
    // referenced by panelToPrint and all of its components
    // to be printed. (See earlier comments regarding 
    // whether the container or just its contents should
    // be printed.)
    panel0 = new Panel();
    Label panel0Label = new Label("Panel 0");
    panel0.add(panel0Label);
    TextField panel0TextField = new TextField("TextField");
    panel0.add(panel0TextField);
    panel0.add(new Button("Dummy Button"));
    panel0.setBackground(Color.yellow);
    
    panel1 = new Panel();
    Label panel1Label = new Label("Panel 1");
    panel1.add(panel1Label);
    TextField panel1TextField = new TextField("TextField");
    panel1.add(panel1TextField);
    panel1.add(new Button("One Dummy Button"));
    panel1.add(new Button("Another Dummy Button"));
    panel1.setBackground(Color.pink);

    //Need a valid reference in panelToPrint to prevent a
    // null pointer exception when a selection is made and
    // an attempt is made to remove the old reference.
    panelToPrint = panel0;

    myFrame.setSize(340,200);
    myFrame.setVisible(true);

    //This is an anonymous inner class of the GUI class 
    // used to terminate the program when the user closes
    // the Frame
    myFrame.addWindowListener(
      new WindowAdapter(){//anonymous class definition
        public void windowClosing(WindowEvent e){
          System.exit(0);//terminate the program
        }//end windowClosing()
      }//end WindowAdapter
    );//end addWindowListener

        }//end constructor
  //=====================================================//
  
  //This is an inner class of the GUI class used to cause
  // the container referenced by panelToPrint or the Frame
  // referenced by myFrame to be printed.
  class PrintActionListener implements ActionListener{

    public void actionPerformed(ActionEvent e){
      //Get a PrintJob object.  This causes the system-
      // standard print dialog to appear.  Closing the
      // dialog without allowing printing will return null.
      PrintJob myPrintJob = myFrame.getToolkit().
            getPrintJob(
              myFrame, "Copyright 1998 R.G.Baldwin", null);
      if(myPrintJob != null){
        //Get a graphics object for printing
        Graphics printGraphics = myPrintJob.getGraphics();
        if(printGraphics != null){
          //Invoke the printAll() method of the Panel
          // object or the Frame object to cause the 
          // components contained in the Panel or the Frame
          // to be drawn on the graphics object and painted
          // onto the paper in the printer.
          if(e.getActionCommand().equals("Print the Panel"))
            panelToPrint.printAll(printGraphics);
          else myFrame.printAll(printGraphics);

          //Cause a form feed on the printer and free
          // the resources tied up by the graphics object.
          printGraphics.dispose();
        }//end if statement
        else 
          System.out.println(
                       "Didn't get print graphics object");
        //End the print job and do any necessary cleanup.
        myPrintJob.end();
      }//end if statement
      else 
        System.out.println("PrintJob cancelled by user");

    }//end actionPerformed()
  }//end class PrintActionListener
  //=====================================================//
    
  //This is an inner class of the GUI class used to cause
  // panel0 to be selected for display and printing.
  class SelectPanel0Listener implements ActionListener{

    public void actionPerformed(ActionEvent e){
      myFrame.remove(panelToPrint);
      panelToPrint = panel0;
      myFrame.add(panelToPrint,"Center");
      myFrame.invalidate();//force a redraw
//      myFrame.repaint();
      //Note that repaint() can be used here instead of
      // setVisible() if setVisible() has been previously
      // invoked on the Frame containing this panel at
      // some previous time in the program.  I don't know
      // which is most efficient.  If repaint() is most
      // efficient, one possibility would be to cycle 
      // through all the possible panels when the program
      // starts invoking setVisible(true) for each of them
      // and then using repaint() here instead of 
      // setVisible().
      myFrame.setVisible(true);
    }//end actionPerformed()
  }//end class PrintActionListener
  //=====================================================//

  //This is an inner class of the GUI class used to cause
  // panel1 to be selected for display and printing.
  class SelectPanel1Listener implements ActionListener{

    public void actionPerformed(ActionEvent e){
      myFrame.remove(panelToPrint);
      panelToPrint = panel1;
      myFrame.add(panelToPrint,"Center");
      myFrame.invalidate();//force a redraw
//      myFrame.repaint(); //see comment above
      myFrame.setVisible(true);
    }//end actionPerformed()
  }//end class PrintActionListener
  //=====================================================//

}//end class GUI
//=======================================================//

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 05-11-03 / 19:26  נוצר ע"י רונית רייכמן  בתאריך 
 מקטעי קוד מעניינים עבור המתודה printAll - הקודםהבא - תוכנית הדגמה לשם צביעה על הנייר עם AWT 
תגובות הקוראים    תגובות  -  0
דרכונט
מהי מערכת הדרכונט?
אינך מחובר, להתחברות:
דוא"ל
ססמא
נושאי לימוד
חיפוש  |  לא פועל
משלנו  |  לא פועל
גולשים מקוונים: 5