java – 动态生成收据

     final JFrame frame = new JFrame("Bill");
    frame.getContentPane().setBackground(Color.WHITE);
     JLabel jlabel = new JLabel("");
        jlabel.setFont(new Font("Serif", Font.PLAIN, 12));
        frame.add(jlabel);
        frame.validate();
        frame.repaint();
        jlabel = new JLabel("-----------BILL-----------");
        jlabel.setFont(new Font("Serif", Font.PLAIN, 20));
        frame.add(jlabel);
        frame.validate();
        frame.repaint();

        jlabel = new JLabel("");
        jlabel.setFont(new Font("Serif", Font.PLAIN, 12));
        frame.add(jlabel);
        frame.validate();
        frame.repaint();
    frame.setLayout(new GridLayout(0, 3));
    try {

        String sql = "SELECT * FROM order_detail";
        rs = st.executeQuery(sql);
        frame.add(new JLabel("Item"));
            frame.validate();
            frame.repaint();
            frame.add(new JLabel("Quantity"));
            frame.validate();
            frame.repaint();
            frame.add(new JLabel("Amount"));
            frame.validate();
            frame.repaint();
        while (rs.next()) {
            String printLbl = rs.getString("Item");
            frame.add(new JLabel(printLbl));
            frame.validate();
            frame.repaint();
            printLbl =""+rs.getInt("Quantity");
            frame.add(new JLabel(printLbl));
            frame.validate();
            frame.repaint();
            printLbl = ""+rs.getInt("Cost");
            frame.add(new JLabel(printLbl));
            frame.validate();
            frame.repaint();

        }
        frame.add(new JLabel(""));            
        frame.validate();
        frame.repaint();
        frame.add(new JLabel("Total"));            
        frame.validate();
        frame.repaint();
        frame.add(new JLabel(jLabel82.getText()));            
        frame.validate();
        frame.repaint();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(700, 400);
    frame.setVisible(true);
    PrinterJob job = PrinterJob.getPrinterJob();
    job.setJobName("frame");
    job.setPrintable(new Printable() {
        public int print(Graphics pg, PageFormat pf, int pageNum) {
            if (pageNum > 0) {
                return Printable.NO_SUCH_PAGE;
            }

            Graphics2D g2 = (Graphics2D) pg;
            g2.translate(pf.getImageableX(), pf.getImageableY());
            frame.paint(g2);
            return Printable.PAGE_EXISTS;
        }
    });
    boolean ok = job.printDialog();
    if (ok) {
        try {
            job.print();
        } catch (PrinterException ex) {

        }

我想创建一个账单收据,其中将生成从数据库账单中提取的项目.我已经使用了上面的代码,但我遇到的问题是它的格式.如何以正确的账单收据格式?

最佳答案

package com.inse6260.atms.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Receipt
{

 public Receipt(String receiptText, JFrame parent)
 {
    final JDialog dialog = new JDialog(parent,"TRANSACTION RECEIPT");
    JTextArea textArea = new JTextArea(100, 20);
    textArea.setEnabled(false);
    textArea.setDisabledTextColor(Color.BLACK);
    textArea.setText(receiptText);
    JButton closeButton = new JButton("Close");
    closeButton.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    JPanel closePanel = new JPanel();
    closePanel.setLayout(new BoxLayout(closePanel, BoxLayout.LINE_AXIS));
    closePanel.add(Box.createHorizontalGlue());
    closePanel.add(closeButton);
    closePanel.setBorder(BorderFactory.createEmptyBorder(0,0,5,5));
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.add(textArea, BorderLayout.CENTER);
    contentPane.add(closePanel, BorderLayout.PAGE_END);
    contentPane.setOpaque(true);
    dialog.setContentPane(contentPane);
    dialog.setSize(300, 130);
    dialog.setLocationRelativeTo(parent);
    dialog.setVisible(true);
}
}

请尝试此代码,它可能会帮助您.

点赞