Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 About print text of textarea problem
Butterfly_125





发贴: 26
积分: 0
于 2005-07-23 03:41 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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




从Coding Fan到真正的技术专家

话题树型展开
人气 标题 作者 字数 发贴时间
2611 About print text of textarea problem Butterfly_125 4154 2005-07-23 03:41

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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