开发者

when is the print method of printable interface called in this code and ...?

// Program to print simple text on a Printer

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.awt.print.*;

class Printer extends JPanel implements Printable  {

  JButton print;

  Printer() {
    buildGUI();
    hookUpEvents();
  }

  public void buildGUI() {
    JFrame fr = new JFrame("Program to Print on a Printer");
    JPanel p = new JPanel();
    print = new JButton("Print");
    setPreferredSize( new Dimension ( 200,200 ) );
    p.setBackground( Color.black );
    fr.add(p);
    p.add( print , BorderLayout.CENTER );
    fr.pack();
    fr.setVisible( true );
  }

  public void hookUpEvents() {
    print.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent ae ) {
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPrintable( new Printer() );
        boolean doPrint = job.printDialog();
        if( doPrint ) {
          try {
            job.print();
          } catch( PrinterException exc) {
            System.out.println( exc );
          }
        } else {
          System.out.println("You cancelled the print");
        } 
      }
    });
  }

  public int print( Graphics g , PageFormat pf , int pageIndex) throws PrinterException{
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    g.drawString( "testi开发者_开发知识库ng..." , 100 , 100 );
    return PAGE_EXISTS;
  }

  public static void main( String args[] ) {
    new Printer();
  }
}

After the command java Printer the output is :

when is the print method of printable interface called in this code and ...?

But as i click print , along with the following window :

when is the print method of printable interface called in this code and ...?

i again get the former window. Why does this happen ?

When does the print method of Printable interface get called in this program?

Why does my window size come small when i have set =200,200.


All right, so you had three questions:

First: The reason why your window is small is because you're using jf.pack() along with a border layout, which makes your JFrame the size required to display all the component it contains without extra space around. You could set the layout to null and lose the jf.pack() part, but this is not usually a recommended practice.

Second: The window pops up a second time because you're creating a second instance of the same object in your listener there:

job.setPrintable( new Printer() );

The "new Printer()" part creates another Printer object, which calls the UI creation again, etc.

You could create an inner class instead of an anonymous class to be able to use "this" to refer to the current Printer object.

Your hookUpEvents() method would be something like this:

public void hookUpEvents() {
  MyActionListener mal = new MyActionListener();
  print.addActionListener(mal); {
}

Then, somewhere else in the same class create the inner class like this:

private class MyActionListener implements ActionListener{
  public void actionPerformed(ActionEvent ae) {
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(Printer.this);
    boolean doPrint = job.printDialog();
     if( doPrint ) {
       try {
        job.print();
       }  
       catch( PrinterException exc) {
           System.out.println( exc );
       }
     }  
     else {
        System.out.println("You cancelled the print"); 
      } 
    }
}

Third: The print() method is called when you type: "job.print()" since you're overriding the method from the interface. The one in the interface is never actually called since yours is called instead, which is what you want since you defined what it had to do.

I hope this answers your questions now.

Edit: I just tested something similar and I think you could just type Printer.this in the anonymous class to make it work instead of creating an inner class, which would have you change way less code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜