gpt4 book ai didi

java - 如何通过在java中选择打印机选项使用按钮打印页面

转载 作者:行者123 更新时间:2023-12-01 23:48:43 28 4
gpt4 key购买 nike

我想通过单击按钮来打印页面,但以下代码显示运行时错误。我使用 JFrame 表单作为打印页面,并添加了 JButton 和 JTextField。查找符号(JButton 和 JTextField)时出错。有人可以帮忙吗?

package yelloclient;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.geom.*;

/**
*
* @author Pavan Poudel
*/
@SuppressWarnings("unchecked")

public class PrintDoc extends javax.swing.JFrame {
/**
* Creates new form PrintDoc
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PrintDoc().setVisible(true);
}
});
}
private void PrintDoc() {
initComponents();
}




private void initComponents() {

jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("Print");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jTextField1.setText("Hello, how are you ?");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(164, 164, 164)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PrintDoc pr = new PrintDoc();
PrintForm.printComponent(pr);
}
}
class PrintForm implements Printable {
private Component component;

public static void printComponent(Component c) {
new yelloclient.txtPrint(c).print();
}

public PrintForm(Component component) {
this.component = component;
}


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

public int print(Graphics g, PageFormat pf, int pageIndex) {
RepaintManager currentManager = RepaintManager.currentManager(component);
if (pageIndex > 0)
{
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/** Disable double buffering as double buffering slows the printing*/
currentManager.setDoubleBufferingEnabled(false);
component.paint(g2d);
currentManager.setDoubleBufferingEnabled(true);
return(PAGE_EXISTS);
}
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}

最佳答案

错误在函数内部

private void initComponents(){ ... } 

这样

cannot find symbol
symbol: variable jButton1
location: class PrintDoc
----
(Alt-Enter shows hints)

等等。

关于java - 如何通过在java中选择打印机选项使用按钮打印页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16615988/

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