0
我想打印一個JPanel是在應用程序中,應用程序內的JPanel顯示下方(注意水平和垂直滾動條):如何打印整個的JPanel Java中
當應用程序運行它的過程中,面板的高度變得非常長,但是當我打印它時,它不打印整個寬度和高度。
此外,面板在紙張中的位置與左側對齊,而不是中心。 下圖顯示了面板如何顯示在紙上,這看起來很糟糕,所以請我需要幫助。
1)如何打印整個面板?
2)如何將它定位在紙張上?
這裏是我的代碼:
public void printWork()
{
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setJobName(" Opt De Solver Printing ");
pj.setPrintable(new Printable()
{
@Override
public int print(Graphics pg, PageFormat pf, int pageNum)
{
if(pageNum > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D)pg;
g2.translate(pf.getImageableX(), pf.getImageableY());
MainAppPanel.this.paintAll(g2);
/* I've tried the following commented codes but they don't work */
//MainAppPanel.this.printAll(g2);
//MainAppPanel.this.print(g2);
//MainAppPanel.this.print(g2);
return Printable.PAGE_EXISTS;
}
});
if(pj.printDialog() == false)
return;
try
{
pj.print();
}
catch(PrinterException xcp)
{
xcp.printStackTrace(System.err);
}
}
看一看[此示例](http://stackoverflow.com/questions/12764634/printing-a-jframe和它的組件/ 12765916#12765916)和[此示例](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on -the-print/22244116#22244116)和[this example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the -print/22242658#22242658)和[this example](http://stackoverflow.com/questions/22058738/print-jlabels-icon-in-a-printer-using-a-button/22059079#22059079) – MadProgrammer
和[這個例子](http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed/17961911#17961911) – MadProgrammer
另外,瞭解改變「活」組件的位置和大小會影響它在屏幕上的繪製效果 – MadProgrammer