Java 如何将透明标题JTable打印到PDF文件
我坚持打印JTable to PDF文件,因为: 这是我的JTable代码:Java 如何将透明标题JTable打印到PDF文件,java,netbeans,printing,header,jtable,Java,Netbeans,Printing,Header,Jtable,我坚持打印JTable to PDF文件,因为: 这是我的JTable代码:JTable.setShowGrid(true); JTable.set不透明(false); ((DefaultTableCellRenderer)JTable.getDefaultRenderer(Object.class)).set不透明(false) 我的打印代码: public static class Printer implements Printable { final Component comp
JTable.setShowGrid(true);
JTable.set不透明(false);
((DefaultTableCellRenderer)JTable.getDefaultRenderer(Object.class)).set不透明(false)代码>
我的打印代码:
public static class Printer implements Printable {
final Component comp;
public Printer(Component comp){
this.comp = comp;
}
@Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}
}
请帮帮我。谢谢大家 你到底是怎么打印表格的?当表格显示在屏幕上时,您是如何设置它的?嗯,JTable.getTableHeader().setBackground(新颜色(0,0,0,0))
是一个问题,我很惊讶你没有其他问题,Swing不支持这种基于alpha的颜色,它只支持完全透明或完全不透明的组件,你可以假装透明,但不是这样。考虑使用<代码> SETPUTIQUE(FALSE)< /代码>,但你不应该意识到<代码> JTAB/<代码>有它自己的,它更容易使用,然后你正在做什么……同样,在打印时,您应该使用print
或printAll
而不是paint
我将JTable放在面板中,并打印该面板中的所有组件,而不仅仅是JTable。你能帮我修复我的透明标题代码吗?你如何处理多个页面?你到底是如何打印表格的?当表格显示在屏幕上时,您是如何设置它的?嗯,JTable.getTableHeader().setBackground(新颜色(0,0,0,0))
是一个问题,我很惊讶你没有其他问题,Swing不支持这种基于alpha的颜色,它只支持完全透明或完全不透明的组件,你可以假装透明,但不是这样。考虑使用<代码> SETPUTIQUE(FALSE)< /代码>,但你不应该意识到<代码> JTAB/<代码>有它自己的,它更容易使用,然后你正在做什么……同样,在打印时,您应该使用print
或printAll
而不是paint
我将JTable放在面板中,并打印该面板中的所有组件,而不仅仅是JTable。你能帮我修复我的透明标题代码吗?你如何处理多个页面?
public static class Printer implements Printable {
final Component comp;
public Printer(Component comp){
this.comp = comp;
}
@Override
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException {
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = comp.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = format.getImageableHeight();
double pWidth = format.getImageableWidth();
double pXStart = format.getImageableX();
double pYStart = format.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) g;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
comp.paint(g2);
return Printable.PAGE_EXISTS;
}