我正在使用正在生成打印頁面的面板和組件打印文本和圖像等各種元素,並且打印也即將到來,但硬打印也在物理紙張上具有打印按鈕。我從頁面中刪除打印按鈕。這裏是代碼在java中的實際硬拷貝上發生打印按鈕
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
public class Printing extends JFrame
implements ActionListener {
public static void main(String[] args)
{
// intialise
}
public Printing(String Firstname,String LastName,String contactid)
{
super("Print badge");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
JButton printButton = new JButton("Print");
printButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.BLACK);
buttonPanel.add(printButton);
content.add(buttonPanel, BorderLayout.SOUTH);
DrawingPanel drawingPanel = new DrawingPanel(Firstname,LastName,contactid);
content.add(drawingPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
//call for printing
}
}
和代碼用於實際打印的面板
public class DrawingPanel extends JPanel
{
private int fontSize1 = 32;
private Image img1=null;
public DrawingPanel(String n1,String n2,String n3)
{
String path="D:"+"\\25175.jpg";
setBackground(Color.white);
Font font = new Font("Serif", Font.PLAIN, 32);
setFont(font);
img1=new ImageIcon(path).getImage();
setPreferredSize(new Dimension(400, 400));
}
public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D)g;
g2d.translate(x, y);
g2d.setPaint(Color.lightGray);
AffineTransform origTransform = g2d.getTransform();
g2d.shear(-0.95, 0);
g2d.scale(1, 3);
g2d.setTransform(origTransform);
g2d.setPaint(Color.BLUE);
g2d.drawString(string,25 , 50);
g2d.drawString(string, 125,100);
g.drawImage(img1, 280, 190, null);
}
}
印刷方法設置在這裏
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintUtilities implements Printable {
private Component componentToBePrinted;
public static void printComponent(Component c) {
new PrintUtilities(c).print();
}
public PrintUtilities(Component componentToBePrinted) {
this.componentToBePrinted = componentToBePrinted;
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException e) {
System.out.println("Error printing: " + e);
}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0) {
return(NO_SUCH_PAGE);
} else {
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.currentManager(c);
currentManager.setDoubleBufferingEnabled(false);
}
public static void enableDoubleBuffering(Component c) {
RepaintManager currentManager = RepaintManager.currentManager(c);
currentManager.setDoubleBufferingEnabled(true);
}
}
你似乎已經離開了所有可能向我們展示的重要印刷的東西,爲什麼你似乎得到CON帳篷窗格的框架打印,而不僅僅是我們的繪圖面板... – MadProgrammer
我已經添加了打印設置的代碼和打印方法 – MandarCT
那麼您實際發送給這個類的組件是什麼? – MadProgrammer