gpt4 book ai didi

java - 用java设计热敏打印机收据

转载 作者:太空宇宙 更新时间:2023-11-04 15:20:36 26 4
gpt4 key购买 nike

我在 Android 中有一个项目,其中我制作了一个类似于购物 list 应用程序的东西,可以打印到收据热敏打印机。我设计收据的方式很简单:我创建了一个 .xml 布局,其中为标题添加了 1 个线性布局,为列添加了 3 个垂直线性布局,为页脚添加了另一个布局。之后,我只需以编程方式将文本添加到布局中,然后然后将 .xml 发送到打印。太棒了,我可以轻松设置字体,轻松移动内容,添加图像、 Logo 等任何内容。

现在,我知道我可以使用graphics2d并打印GUI组件,我用文本区域测试了它,它工作得很好。当你有大约100行时它甚至会滚动,不像jframes,它只打印它所看到的内容。

我的问题是我需要 3-4 列文本。如下所示:

1. Bananas         4     11.20$
2. Toilet paper 12 3.99$
3. Donuts 1 0.49$

我可以将其添加到文本区域并打印它,但问题是文本的长度会有所不同,并且文本会变得困惑且不均匀。

那么,我怎样才能像在android中那样做:添加3-4列,我可以在其中添加文本,即使它们有100行,打印机也将成功打印所有行,而不是您无需向下滚动即可看到的行?

提前致谢,祝你有美好的一天!

最佳答案

经过近 6 年的时间,我终于有时间再次打开该项目,并考虑发布我最终使用的解决方案,所以这里是:

@Log4j2
@Service
public class PrintService implements Printable {

@Autowired
private PreviewManager previewManager;

public boolean printCopies() {

final JPanel printPanel = new JPanel();
DefaultListModel listModel = new DefaultListModel();

printPanel.setLayout(new BoxLayout(printPanel, BoxLayout.PAGE_AXIS));
printPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JLabel label = new JLabel("Select the number of copies");
listModel.addElement("0");
listModel.addElement("1");
listModel.addElement("2");
listModel.addElement("3");
listModel.addElement("4");
listModel.addElement("5");

final JList list = new JList(listModel);
list.setVisibleRowCount(1);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

final int[] nrOfCopies = {1};
list.addListSelectionListener(lse -> nrOfCopies[0] = Integer.parseInt((String) list.getSelectedValue()));
printPanel.add(label);

printPanel.add(list);
label.setAlignmentX(JComponent.CENTER_ALIGNMENT);
list.setAlignmentX(JComponent.CENTER_ALIGNMENT);
list.setSelectedIndex(1);
JOptionPane jop = new JOptionPane(printPanel,
JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
JDialog dialog = jop.createDialog(EMPTY_STRING);
dialog.setAlwaysOnTop(true);
dialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
SwingUtilities.invokeLater(list::requestFocusInWindow);
}
});
dialog.setVisible(true);
int result = (Integer) jop.getValue();
dialog.dispose();

if (result == JOptionPane.OK_OPTION) {
log.info("Printing {} copies of the list", nrOfCopies[0]);
for (int c = 0; c < nrOfCopies[0]; c++) {
print();
}
return true;
}
// todo: bring back
return false;
}

public void print() {
PrinterJob pjob = PrinterJob.getPrinterJob();
javax.print.PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
for (javax.print.PrintService printer : printServices) {
// in my case I use a Star TSP143 thermal printer
if (printer.getName().contains("tsp143")) {
try {
pjob.setPrintService(printer);
} catch (PrinterException e) {
log.error(e);
}
}
}
PageFormat preformat = pjob.defaultPage();
preformat.setOrientation(PageFormat.PORTRAIT);
PageFormat postformat = pjob.defaultPage(preformat);
Paper p = preformat.getPaper();
p.setImageableArea(0, 0, postformat.getWidth(), postformat.getHeight());
postformat.setPaper(p);

//If user does not hit cancel then print.
if (preformat != postformat) {
//Set print component
pjob.setPrintable(PrintService.this, postformat);
try {
pjob.print();
} catch (PrinterException ex) {
log.error(ex);
}
}
}


@Override
public int print(Graphics g, PageFormat format, int page_index) {
// depends on your use case, printableJpanel is going to be the panel you are going to print
JPanel printableJpanel = previewManager.getPrintablePanel();
if (page_index > 0) {
return Printable.NO_SUCH_PAGE;
}

// get the bounds of the component
Dimension dim = printableJpanel.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, 0.80);
printableJpanel.paint(g2);

return Printable.PAGE_EXISTS;

}
}

祝你好运!

关于java - 用java设计热敏打印机收据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20435840/

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