Topic: About print text of textarea problem

  Print this page

1.About print text of textarea problem Copy to clipboard
Posted by: Butterfly_125
Posted on: 2005-07-23 03:41

I have a jtextarea in jpanel with more than 40 lines. I have given a button to print data of this textarea. when i click button follwing function is called

PrintUtility.printComponent(JTextArea_obj);

All class given below

/**
* @author J_Butterfly
* Created on 2005-7-22
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.print.*;

public class MathKit extends JFrame implements ActionListener,Printable
{
  static JButton chuti = new JButton("出题");
  static JButton print = new JButton("打印");
  
  static JTextArea ta = new JTextArea();
  static String s = ("");
  Random r1 = new Random();
  int first = 0;
  int sec = 0,answer;
  int num = 30;
  
  String printStr = ("");
  Font f = new Font("monospaced", Font.BOLD, 20);
  
  static MathKit mk = new MathKit();  
  public void init()
  {
    mk.setTitle("加减法自动生成器");
    mk.setBackground(Color.lightGray);
    mk.setSize(800,600);
    mk.setLayout(null);
    ta.setBackground(Color.white);
    ta.setBounds(5,5,mk.getSize().width-60,mk.getSize().height-70);
    ta.setEditable(false);
    ta.setFontRose;
    ta.setTextMoon;
    //ta.show();
    chuti.setBounds(mk.getSize().width-90,mk.getSize().height-55,70,20);
    print.setBounds(mk.getSize().width-180,mk.getSize().height-55,70,20);
    chuti.addActionListener(mk);
    print.addActionListener(mk);
    mk.add(chuti);
    mk.add(print);
    
    mk.add(ta);
    mk.setVisible(true);
  }
  public static void main (String args [])
  {
    mk.init();
  }
  
  public int print(Graphics g, PageFormat pf, int pageIndex) {
   if (pageIndex != 0)
   return NO_SUCH_PAGE;
   Graphics2D g2 = (Graphics2D) g;
   g2.setFont(new Font("Serif", Font.PLAIN, 16));
   g2.setPaint(Color.black);
   g2.drawString(s, 144, 144);
   return PAGE_EXISTS;
   }
  
  public void creat()
  {
    int j = 1;
    for(int i = 0;i < num;i++)
    {
    first = r1.nextInt(5) + 1;
    sec = r1.nextInt(5) + 1;
    answer = first + sec;
    if(j%3 != 0)
    {
      s = s + "\t" + first + " + " + sec +" = "+"\t";
      j++;
    }
    else
    {
      s = s + "\t" + first + " + " + sec +" = "+"\n";
      j++;
    }
    }
  }
  
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource() == chuti)
    {
      s = "";
      num = 50;
      mk.creat();
      ta.setTextMoon;
      
    }
    if(e.getSource() == print)
    {
      PrintUtility printUtility = new PrintUtility(ta);
      printUtility.printComponent(ta);
    }
    
  }
  
}
class PrintUtility implements Printable {
private Component componentToBePrinted;

public void printComponent(Component c) {
  new PrintUtilityCoffee.print();
}

public PrintUtility(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
}

public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error printing: " + pe);
}
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex != 0) {
return(NO_SUCH_PAGE);
} else {
   Graphics g2 = componentToBePrinted.getGraphics();
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
disableDoubleBuffering(componentToBePrinted);
componentToBePrinted.paint(g2d);
enableDoubleBuffering(componentToBePrinted);
return(PAGE_EXISTS);
}
}

public static void disableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManagerCoffee;
currentManager.setDoubleBufferingEnabled(false);
}

public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManagerCoffee;
currentManager.setDoubleBufferingEnabled(true);
}
}

after calling above function a print dialog is appeared as appeared in window.
When i click ok button, only in and out ,no printed!

please reply me what is wrong with this code.

Thanks


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923