Printing 如何以两行打印JTable标头?

Printing 如何以两行打印JTable标头?,printing,Printing,程序是打印一个JTabel,使用的函数是 JTabel jt=new JTable(); MessageFormat headerFormat= new MessageFormat("My World Tomorrow"); MessageFormat footerFormat = new MessageFormat("Page {0}"); jt.Print(JTabel.Format,headerFormat,footerFormat); 查询是:如何打印两行标题,即 My World

程序是打印一个JTabel,使用的函数是

JTabel jt=new JTable();
MessageFormat headerFormat= new MessageFormat("My World Tomorrow");
MessageFormat footerFormat = new  MessageFormat("Page {0}");

jt.Print(JTabel.Format,headerFormat,footerFormat);
查询是:如何打印两行标题,即

My World 
Tomorrow
以下解决方案:

new MessageFormat("My world \n Tomorrow");
new MessageFormat("My world \r\n Tomorrow");
new MessageFormat("My world" System.getProperty("line.separator")+"Tomorrow"
))

什么都不管用。

试试这个

PrinterJob job = PrinterJob.getPrinterJob();
MessageFormat[] header = new MessageFormat[3];
header[0] = new MessageFormat("");
header[1] = new MessageFormat("line 1");
header[2] = new MessageFormat("line 2");

MessageFormat[] footer = new MessageFormat[2];
footer[0] = new MessageFormat("footer 1");
footer[1] = new MessageFormat("footer 2");
job.setPrintable(new MyTablePrintable(tblmunim, PrintMode.FIT_WIDTH, header, footer));

if (job.printDialog())
  try {
    System.out.println("Calling PrintJob.print()");
    job.print();
    System.out.println("End PrintJob.print()");
  }
  catch (PrinterException pe) {
    System.out.println("Error printing: " + pe);
  }

最初发布在这里

欢迎来到StackOverflow Eruls。发布代码时,将其缩进4个空格或高亮显示代码,并在编辑问题时按“代码格式”按钮。使它更易于阅读,并增加了获得答案的机会。您是否尝试过一个反斜杠()?如果没有MyTablePrintable实现,这将是无用的,而这正是神奇的地方。您好,我很久没有在秋千上工作了,但您可以尝试一下。1) 获取TablePrintable的代码,并将其复制到您自己的类“MyTablePrintable”中。2) 在需要的地方修改构造函数。