gpt4 book ai didi

Java Swing Invoice String.format 仅将 for () {} 值返回到 JTextPane?

转载 作者:行者123 更新时间:2023-12-01 09:52:59 24 4
gpt4 key购买 nike

打印机作业仅打印 JTable 中的一行。输入一行或多行只能得到一个输出:

public void invoice_job()   {
try {

int rows=JTable.getRowCount();

for (int row = 0; row<rows; row++) {

Names = (String)JTable.getValueAt(row, 0);
Quanti = (String)JTable.getValueAt(row, 1);
Prices = (Double)JTable.getValueAt(row, 2);

String head = String.format("%30s\n\n", " Invoice Reciept ");
String s = String.format("%-15s %5s %10s\n", "Item", "Qty", "Price");
String s1 = String.format("%-15s %5s %10s\n","---------","--------"," ----------");
String line = String.format("%-15s %5s %10.2f\n",Names,Quanti,Prices);
String lin2 = String.format("%-15s\n","-------------------------------------");
String Total = String.format("%37s\n","Total");
String total = String.format("%37s\n\n",lbl.getText());
String footer = String.format("%-15s\n", "Meridon sweets and bakers\nSince 1987\nPoint of sale system\nPowered by msofttech\nwww.msofttech.net");
String output = head + s + s1 + line + lin2 + Total + total + footer;
textPane.setText(output);
textPane.setText(output);
textPane.setText(output);
textPane.setText(output);
JOptionPane.showMessageDialog(null, output);

}

PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.defaultPage();
Paper paper = new Paper();
paper.setSize(180.0, (double) (paper.getHeight() + rows * 10.0));
paper.setImageableArea(rows, rows, paper.getWidth() - rows * 2,
paper.getHeight() - rows * 2);
pageFormat.setPaper(paper);
pageFormat.setOrientation(PageFormat.PORTRAIT);
printerJob.setPrintable(textPane.getPrintable(null, null), pageFormat);
printerJob.print();

}
catch (Exception e) {
e.printStackTrace();
}

输入:

image

输入 2 项。

输出:

image

输出仅一项。

最佳答案

尝试下面的代码:

   public void invoice_job()   
{



try
{

int rows=JTable.getRowCount();
String head = String.format("%30s\n\n", " Invoice Reciept ");
String s = String.format("%-15s %5s %10s\n", "Item", "Qty", "Price");
String s1 = String.format("%-15s %5s %10s\n","---------","--------"," ----------");
String output = head + s + s1;
for(int row = 0; row<rows; row++)

{

String Names = (String)JTable.getValueAt(row, 0);
String Quanti = (String)JTable.getValueAt(row, 1);
String Prices = (Double)JTable.getValueAt(row, 2);

String line = String.format("%-15s %5s %10.2f\n",Names,Quanti,Prices);
output += line;
String lin2 = String.format("%-15s\n","-------------------------------------");
output += lin2;



}

String Total = String.format("%37s\n","Total");
String total = String.format("%37s\n\n",lbl.getText());
String footer = String.format("%-15s\n", "Meridon sweets and bakers\nSince 1987\nPoint of sale system\nPowered by msofttech\nwww.msofttech.net");
output += Total + total + footer;
textPane.setText(output);

JOptionPane.showMessageDialog(null, output);


PrinterJob printerJob = PrinterJob.getPrinterJob();
PageFormat pageFormat = printerJob.defaultPage();
Paper paper = new Paper();
paper.setSize(180.0, (double) (paper.getHeight() + rows * 10.0));
paper.setImageableArea(rows, rows, paper.getWidth() - rows * 2, paper.getHeight() - rows * 2);
pageFormat.setPaper(paper);
pageFormat.setOrientation(PageFormat.PORTRAIT);
printerJob.setPrintable(textPane.getPrintable(null, null), pageFormat);
printerJob.print();

}
catch ( Exception e )
{

e.printStackTrace();
}
}

您需要在文本 Pane 中为每一行添加一行。

关于Java Swing Invoice String.format 仅将 for () {} 值返回到 JTextPane?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com