我想在我的Java GUI揮杆中打印某個部分。所以基本上我有一個框架,然後像文本框,標籤,表格和麪板等其他視圖。在面板內部,我有兩個按鈕。我想排除打印包含兩個按鈕的面板。我怎樣才能做到這一點?到目前爲止,這是我所做的代碼:Java僅打印揮杆中的特定部分
setTitle("VMS Sales Invoice");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 397, 628);
getContentPane().setLayout(null);
JLabel lblVatable = new JLabel("VATable");
lblVatable.setFont(new Font("SansSerif", Font.BOLD, 12));
lblVatable.setBounds(158, 468, 55, 16);
getContentPane().add(lblVatable);
txtTotalDiscount = new JTextField();
txtTotalDiscount.setFont(new Font("SansSerif", Font.BOLD, 12));
txtTotalDiscount.setEditable(false);
txtTotalDiscount.setBounds(256, 485, 122, 28);
getContentPane().add(txtTotalDiscount);
txtTotalDiscount.setColumns(10);
JLabel lblTotalDiscount = new JLabel("Total Discount");
lblTotalDiscount.setFont(new Font("SansSerif", Font.BOLD, 12));
lblTotalDiscount.setBounds(158, 496, 86, 16);
getContentPane().add(lblTotalDiscount);
setResizable(false);
setLocationRelativeTo(null); // THIS WILL CENTRE THE POSITION OF WINDOW
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
public class btnPrintAction implements ActionListener, Printable{
public int print(Graphics gx, PageFormat pf, int page) throws PrinterException {
if (page > 0){
return NO_SUCH_PAGE;
} // Only one page
Graphics2D g = (Graphics2D)gx; // Cast to Graphics2D object
g.translate(pf.getImageableX(), pf.getImageableY()); // Match origins to imageable area
//g.drawString ("Hello world", 100, 100); // Print Hello World at offset (100, 100)
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
// Print the entire visible contents of a
// java.awt.Frame.
getContentPane().printAll(g);
return PAGE_EXISTS; // Page exists (offsets start at zero!)
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob(); // Get the printer's job list
job.setPrintable(this); // We print with this class (btnPrintAction, which implements Printable)
if (job.printDialog() == true) { // If we clicked OK in the print dialog
try {
job.print();
} catch (PrinterException ex){
// It did not work (PrinterException thrown), so add any error handling routines.
JOptionPane.showMessageDialog(null, ex.toString(), "Printing Error",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
你有什麼想法如何實現這一點?我需要得到這個工作。非常感謝幫助。謝謝。
分割你的用戶界面爲不同的區域(跨多個組件),您想要打印的組件和那些不這樣做,那麼只打印你想要打印的面板... – MadProgrammer