gpt4 book ai didi

java - 如何打印 Java 书籍?

转载 作者:行者123 更新时间:2023-12-01 15:39:41 26 4
gpt4 key购买 nike

下面代码的问题是它不打印 bk (Java Book),而只打印“g”,即用户界面窗口。

有人可以解释一下如何打印 Java 书吗?

public class printInvoice extends javax.swing.JFrame implements Printable {

JFrame frameToPrint;


/** Creates new form SimplePrint */
public void SimplePrint(JFrame f) {
frameToPrint = f;

}

/** Creates new form NewJFrame */
public printInvoice() {
initComponents();
this.setVisible(true);

print2();

}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new printInvoice().setVisible(true);
}
});
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return (NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
// Turn off double buffering
//componentToBePrinted.paint(g2d);
//frameToPrint.print(g);
paint(g);
// this.print(g);

// this.print(g);

// Turn double buffering back on
return (PAGE_EXISTS);
}
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
int userCountAmount;
dataBase data = new dataBase();
Book bk = new Book();
PrinterJob PJ = PrinterJob.getPrinterJob();


public void print2() {
userCountAmount = data.getAmountOfUsers();

PageFormat portrait = PJ.defaultPage();

portrait.setOrientation(PageFormat.PORTRAIT);


jLabel1.setText("Print number: "+0);
bk.append((Printable) this, PJ.defaultPage(),1);
jLabel1.setText("Print number: "+1);
bk.append((Printable) this, PJ.defaultPage(),2);
jLabel1.setText("Print number: "+2);
bk.append((Printable) this, PJ.defaultPage(),3);
System.out.println(bk.toString());

//PageFormat PF = PJ.pageDialog(PJ.defaultPage());
PJ.setPageable(bk);

// PJ.setPrintable((Printable) this);
boolean doPrint = PJ.printDialog();
//JOptionPane.showMessageDialog(null, doPrint);
if (doPrint) {
try {

PJ.print();

} catch (PrinterException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
}
}
}
}

最佳答案

打印书籍时,打印机作业正在使用 print(Graphics g, PageFormat pageFormat, int pageIndex)。您的 print 方法调用 paint(g),这是 UI 框架的方法。

如果您不想打印 UI 控件的图像,则应使用 print 方法的 g 将所需图像绘制到页面。

编辑:示例,将文本绘制到页面中间(未测试):

public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2d = (Graphics2D) g;

double x1 = pf.getImageableX();
double y1 = pf.getImageableY();
double x2 = pf.getImageableWidth();
double y2 = pf.getImageableHeight();

g2d.drawString( "Page #" + pageIndex, (int)((x2-x1)/2+x1), (int)((y2-y1)/2+y1) );

return (PAGE_EXISTS);
}

顺便说一句:

如果您想打印多页,则需要更改代码中的以下位置:

    if (pageIndex > 0) {
return (NO_SUCH_PAGE);

关于java - 如何打印 Java 书籍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8269657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com