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);
}
}
}
}
어떻게 구현할 수 있습니까? 나는이 일을해야한다. 도움을 많이 주시면 감사하겠습니다. 감사.
UI를 여러 섹션으로 나누고 (여러 구성 요소에 걸쳐) 인쇄 할 구성 요소와 그렇지 않은 구성 요소를 위로 나눈 다음 간단히 인쇄 할 패널을 인쇄하십시오. – MadProgrammer