动态生成收据

时间:2014-10-20 08:45:12

标签: java swing awt

     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) {

        }

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

2 个答案:

答案 0 :(得分:1)

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);
}
}

请尝试使用此代码,它可能对您有帮助。

答案 1 :(得分:1)

尽管您的代码可以被清理得很多,但使用JComponents通常有点麻烦,当然只是打印时。创建HTML文本,并将其放在标签中。然后你有更多的造型方法。 Bill using HTML 首先,可以进行良好的格式化。使用MessageFormat可以使用不同的Locales,但至少有一个接收正确的千位分隔符等。

private static String formatInt(int amount) {
    return new MessageFormat("{0,number}").format(new Object[]{amount});
}

private static String formatAmount(int amount) {
    return new MessageFormat("{0,number,currency}").format(new Object[]{amount})
        .replace(' ', '\u00a0');
}

可以从数据库构建HTML字符串:

public static String billHTML() {
    StringBuilder sb = new StringBuilder();
    sb.append("<html>"
            + "<style type='text/css'>"
            + "body, h1, th, td {"
            + "  font-family: Serif;"
            + "  font-size: 12pt;"
            + "}"
            + "h1 {"
            + "  font-size: 20pt;"
            + "}"
            + "table {"
            //+ "  border-collapse: collapse;"
            //+ "  border-style: none;"
            + "}"
            + "td, th {"
            //+ "  border: thin solid gray;"
            + "}"
            + "th {"
            + "  border-bottom: thin solid gray;"
            + "}"
            + ".overline td {"
            + "  border-top: thin solid gray;"
            + "}"
            + "</style>"
            + "<body>");
    sb.append("<h1>- BILL -</h1>");

    String sql = "SELECT * FROM order_detail";
    try (PreparedStatement st = connection.prepareStatement(sql)) {
        try (ResultSet rs = st.executeQuery()) {
            sb.append("<table width='650' cellspacing='0'>"
                    + "<tr>"
                    + "<th width='50%' align='left'>Item</th>"
                    + "<th width='20%' align='right'>Quantity</th>"
                    + "<th width='30%' align='right'>Amount</th>"
                    + "</tr>");
            int total = 0;
            while (rs.next()) {
                sb.append("<tr>"
                        + "<td>")
                    .append(rs.getString("Item"))
                    .append("</td>"
                        + "<td align='right'>")
                    .append(formatInt(rs.getInt("Quantity")))
                    .append("</td>"
                        + "<td align='right'>")
                    .append(formatAmount(rs.getInt("Cost")))
                    .append("</td>"
                        + "</tr>");
                total += rs.getInt("Cost");
            }
            sb.append("<tr class='overline'>"
                    + "<td>&nbsp;")
                .append("</td>"
                    + "<td align='right'>")
                .append("Total")
                .append("</td>"
                    + "<td align='right'>")
                .append(formatAmount(total))
                .append("</td>"
                    + "</tr>"
                + "</table>");
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return sb.toString();
}

    frame.getContentPane().setBackground(Color.WHITE);
    frame.getContentPane().setLayout(new FlowLayout());
    JLabel jlabel = new JLabel(billHTML());
    frame.add(jlabel);

可以使用MessageFormat作为整个表行,而不是附加链接。