我正在制作打印JFrame内容的应用程序。它工作正常但我的问题是我必须在"打印"打印JButton的不同形式的JButton在Form_1.java上,打印内容在Form_2.java上... 我在Print Button上使用ActionListener但它不起作用.. 请帮助我,我的项目是在截止日期... 我在这里发布了我的代码:
public class PrintUI implements Printable, ActionListener {
JFrame frameToPrint;
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY() - 55);
frameToPrint.setVisible(true);
frameToPrint.print(g);
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
}
}
}
public PrintUI(JFrame f) {
frameToPrint = f;
}
public static void main(String args[]) {
final JButton printButton = new JButton("Print");
UIManager.put("swing.boldMetal", Boolean.TRUE);
JFrame f = new JFrame("GatePass");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Font f1 = new Font("Times New Roman", Font.BOLD, 12);
printButton.setVisible(true);
JLabel label1 = new JLabel("Vision Techno Solutions", JLabel.CENTER);
label1.setBounds(30, 30, 200, 40);
JLabel label2 = new JLabel("Batch No:");
label2.setBounds(10, 70, 80, 20);
JLabel label3 = new JLabel("Date and Time:");
label3.setBounds(10, 100, 120, 20);
JLabel label4 = new JLabel("Visitor's Name:");
label4.setBounds(10, 130, 120, 20);
JLabel label5 = new JLabel("Concern Person:");
label5.setBounds(10, 160, 120, 20);
JLabel label6 = new JLabel("Purpose of Visit:");
label6.setBounds(10, 190, 120, 20);
JLabel label7 = new JLabel("Manager's Sign");
label7.setBounds(140, 230, 120, 20);
JLabel label8 = new JLabel("Visitor's Sign");
label8.setBounds(10, 230, 120, 20);
printButton.addActionListener(new PrintUI(f));
f.setSize(300, 350);
f.setLocationRelativeTo(null);
f.add(printButton, BorderLayout.SOUTH);
f.setResizable(false);
f.setVisible(true);
printButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
printButton.setVisible(false);
}
});
}
}
答案 0 :(得分:3)
不确定你到底想要做什么。
你说你正在尝试打印另一帧但
printButton.addActionListener(new PrintUI(f));
表示您要添加PrintUI
类来打印当前帧。
如果要打印另一个框架,请首先在代码中创建另一个框架,使其可见,然后将新框架传递给PrintUI()
,这应该有用。