gpt4 book ai didi

java - 动态生成收据

转载 作者:搜寻专家 更新时间:2023-11-01 03:22:30 25 4
gpt4 key购买 nike

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

请试试这个代码,它可能对你有帮助。

关于java - 动态生成收据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26461662/

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