Hallo,
ich habe ein kleines Programm geschrieben das den Inhalt des Frames ausdrucken soll. Leider wird aber nur der JButton "Print" gedruckt!
Hat zufällig jemand eine Idee an was das liegen könnte?
ich habe ein kleines Programm geschrieben das den Inhalt des Frames ausdrucken soll. Leider wird aber nur der JButton "Print" gedruckt!
Hat zufällig jemand eine Idee an was das liegen könnte?
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
public class PrintPanel extends JFrame{
public PrintPanel(){
setSize(400,300);
setLayout(new BorderLayout());
JComponent anzeigen = new malen();
getContentPane().add(anzeigen,BorderLayout.CENTER);
JPanel druck = new PrPa();
getContentPane().add(druck, BorderLayout.SOUTH);
setVisible(true);
}
public static void main (String[] args){
new PrintPanel();
}
}
class malen extends JComponent {
public void paintComponent(Graphics g){
g.drawRect(10,30,50,80);
g.setColor(Color.RED);
g.fillRect(50,100,200,300);
}
}
class PrPa extends JPanel implements Printable {
JButton btnDialog;
JPanel instance = this;
public PrPa(){
super();
btnDialog = new JButton("Print");
add(btnDialog);
btnDialog.addActionListener(new MyListener());
}
private class MyListener implements ActionListener {
public void actionPerformed(ActionEvent e){
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable((PrPa) instance);
PageFormat pf = printJob.pageDialog(printJob.defaultPage());
if (printJob.printDialog()){
try{
printJob.print();
} catch (Exception PrinterException){
System.out.println(PrinterException);
}
}
}
}
public int print(Graphics g, PageFormat pf, int pi){
if (pi >= 1){
return Printable.NO_SUCH_PAGE;
}
Graphics2D graphics = (Graphics2D)g;
graphics.translate(pf.getImageableX(),pf.getImageableY());
paint(graphics);
return Printable.PAGE_EXISTS;
}
}