这里的布尔值如何自己变成假的 [英] How does boolean here become false on its own

查看:44
本文介绍了这里的布尔值如何自己变成假的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是打印按钮的动作监听器

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

}

当我编译这个片段和整个代码时,打印按钮被显示出来.以上是打印按钮的动作监听器.

When i compile this snippet along with whole code , print button gets displayed . The above is the action listener of the print button.

当我单击打印按钮时,将显示此对话框:

As i click the print button this dialog box is displayed :

3-4 秒后自动 您取消打印 显示在 cmd 上.这是怎么发生的?当我点击取消时,什么也没有显示.语句 job.printDialog(); 如何自己返回 false ?

Automatically after 3-4 seconds You cancelled the print is displayed on the cmd. How does this happen ? And when i click cancel nothing is displayed. How can the statement job.printDialog(); return false on its own ?

完整代码

// 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");
  p.setBackground( Color.black );
  fr.add(p);
  p.add( print , BorderLayout.CENTER );
  this.setPreferredSize( new Dimension ( 300,200 ) );
  fr.pack();
  fr.setVisible( true );

}

public void hookUpEvents() {
 print.addActionListener( new ActionListener() {
   public void actionPerformed( ActionEvent ae ) {
      PrinterJob job = PrinterJob.getPrinterJob();
      job.setPrintable( Printer.this );
      boolean doPrint = job.printDialog();
//  PageFormat pf = job.pageDialog(job.defaultPage());
       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{
   return PAGE_EXISTS;
 }

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

}

推荐答案

根据 API,打印对话框是由操作系统处理的,而不是 JVM,所以我并不完全感到惊讶,不同的人有不同的设置可以有不同的结果.我建议您尝试使用实际打印机运行它,看看会发生什么.

Per the API, the print dialog is handled by the operating system, not the JVM, so I'm not completely surprised that different folks with different set ups could have different results. I suggest you try running it with an actual printer and see what happens.

这篇关于这里的布尔值如何自己变成假的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆