小弟是新手
因工作上需要,設計一個列印的program
因測試後只能列印整個JFrame的component內容(包含title,顏色)
而我的需求是只列印JFrame的JTextField的文字
並且列印在原來JTextField在JFrame相同的位置
各位高手前輩,請教
1.如何只列印JTextfield的文字,且列印在原來JTextfield相同位置
2.如何設定紙張大小
謝謝
=========================================================
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
public class q1 extends JFrame implements Printable,ActionListener{
static JPanel contentPane;
JTextField branchtf1;
JTextField branchadrestf;
JTextField lcnewdate;
String branchtf1_1;
String branchadrestf_1;
String lcnewdate_1;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
q1 frame = new q1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public q1() {
setTitle("PrintJtextfield");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 686, 955);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setSize(700,500);
branchtf1 = new JTextField();
branchtf1.setBounds(222, 15, 203, 29);
contentPane.add(branchtf1);
branchtf1.setColumns(10);
branchadrestf = new JTextField();
branchadrestf.setColumns(10);
branchadrestf.setBounds(222, 59, 203, 29);
contentPane.add(branchadrestf);
lcnewdate = new JTextField();
lcnewdate.setColumns(10);
lcnewdate.setBounds(222, 136, 203, 29);
contentPane.add(lcnewdate);
//列印鍵開始
JButton printbtn = new JButton("\u5217\u5370");
printbtn.setBounds(222, 220, 111, 31);
contentPane.add(printbtn);
printbtn.addActionListener(this);
//列印鍵結束
}
@Override
public void actionPerformed(ActionEvent e) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
public int print(Graphics g, PageFormat pf, int index) throws
PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.setBackground(Color.white);
AffineTransform originalTransform = g2.getTransform();
double scaleX = pf.getImageableWidth() / this.getWidth();
double scaleY = pf.getImageableHeight() / this.getHeight();
// Maintain aspect ratio
double scale = Math.min(scaleX, scaleY);
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.scale(scale, scale);
this.printComponents(g2);
g2.setTransform(originalTransform);
if (index >= 1) {
return Printable.NO_SUCH_PAGE;
} else {
this.printComponents(g2);
return Printable.PAGE_EXISTS;
}
}
}