无法运行 string.format + 退出按钮问题

我正在做我的第一个项目。当我试图转到新行时,string.format它以某种方式失败了(要么根本不打印,要么在同一行打印)。


这是代码:


    String reciept = String.format("Recipt number #16424 +"  + "%n" + "Beef Burgers :" + ab1 +"%n" + "Cheese Burgers :" + ab2 + "%n" +   "Fish and Chips :" + ab3 + "%n"  + "French Fries :" + ab5 + "%n" +  "Steak :" + ab4 + "%n"  + "Sprite Drinks : " + ab + "%n" +  "Soda Drinks : " + ab8 + "%n" + "Fuzetea Drinks : " + ab7 + "%n" + "Coke Drinks :" + ab6 + "%n" , ab,ab1,ab2,ab3,ab4,ab5,ab6,ab7,ab8);

            recieptText.setText(reciept);

还有 1 个问题。我正在尝试创建JButton出口。我试图打开一条消息,说如果我确定退出然后退出,但我失败了,所以我尝试了注册,如果他按下退出,它将退出,我也失败了。


JButton btnExit = new JButton("Exit");

btnExit.setFont(new Font("Tahoma", Font.PLAIN, 19));

btnExit.setBounds(766, 484, 127, 39);

if (btnExit.isSelected()==true) {

    System.exit(0);

}

frame.getContentPane().add(btnExit);


慕田峪4524236
浏览 163回答 1
1回答

跃然一笑

雪,你正处于一段漫长而有趣且富有成果的旅程的开始。祝你好运!对于您的第一个问题,就面向对象设计原则而言,在不同的完善水平上有许多正确的答案。我需要建议你尽早阅读 Java 编码标准、控制反转、单元测试。我将仅提及封装并推荐您以下解决方案。我将首先创建两个类 Receipt 和 ReceiptItem(可以根据项目的范围创建 Product、ProductPrice 等),假设它不仅是您尝试打印的单个收据(并且可能用于存储收据以供以后检查)还需要一个持久层,这是为您探索的另一个领域),但您将处理许多带有更多收据详细信息行的收据。然后我将创建这两个类的实例,并在将其发送到文本显示/打印设备之前调用 Receipt 类的 toFormattedString 方法。public class ReceiptItem {&nbsp; &nbsp; private static final int TITLE_WIDTH = 30;&nbsp; &nbsp; private static final String DETAIL_FORMAT="%-" + TITLE_WIDTH + "s : %6s%n";&nbsp; &nbsp; private String title;&nbsp; &nbsp; private BigDecimal totalPrice;&nbsp; &nbsp; public ReceiptItem(String title, BigDecimal totalPrice) {&nbsp; &nbsp; &nbsp; &nbsp; setTitle(title);&nbsp; &nbsp; &nbsp; &nbsp; setTotalPrice(totalPrice);&nbsp; &nbsp; }&nbsp; &nbsp; public String getTitle() {&nbsp; &nbsp; &nbsp; &nbsp; return title;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTitle(String title) {&nbsp; &nbsp; &nbsp; &nbsp; this.title = title;&nbsp; &nbsp; }&nbsp; &nbsp; public BigDecimal getTotalPrice() {&nbsp; &nbsp; &nbsp; &nbsp; return totalPrice;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTotalPrice(BigDecimal totalPrice) {&nbsp; &nbsp; &nbsp; &nbsp; this.totalPrice = totalPrice;&nbsp; &nbsp; }&nbsp; &nbsp; public String toFormattedString() {&nbsp; &nbsp; &nbsp; &nbsp; return String.format(DETAIL_FORMAT, getTitle(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getTotalPrice().toPlainString());&nbsp; &nbsp; }}public class Receipt {&nbsp; &nbsp; private static final String RECEIPT_HEADER_FORMAT = "Receipt number #%s%n";&nbsp; &nbsp; private static int LAST_RECEIPT_NUMBER = 0;&nbsp; &nbsp; private int receiptNumber;&nbsp; &nbsp; private List<ReceiptItem> items= new ArrayList<>();&nbsp; &nbsp; public Receipt() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; &nbsp; setReceiptNumber(++LAST_RECEIPT_NUMBER);&nbsp; &nbsp; }&nbsp; &nbsp; public Receipt(int receiptNumber) {&nbsp; &nbsp; &nbsp; &nbsp; this();&nbsp; &nbsp; &nbsp; &nbsp; setReceiptNumber(LAST_RECEIPT_NUMBER=receiptNumber);&nbsp; &nbsp; }&nbsp; &nbsp; public void addItem(ReceiptItem item) {&nbsp; &nbsp; &nbsp; &nbsp; items.add(item);&nbsp; &nbsp; }&nbsp; &nbsp; public int getReceiptNumber() {&nbsp; &nbsp; &nbsp; &nbsp; return receiptNumber;&nbsp; &nbsp; }&nbsp; &nbsp; public void setReceiptNumber(int receiptNumber) {&nbsp; &nbsp; &nbsp; &nbsp; this.receiptNumber = receiptNumber;&nbsp; &nbsp; }&nbsp; &nbsp; public List<ReceiptItem> getItems() {&nbsp; &nbsp; &nbsp; &nbsp; return items;&nbsp; &nbsp; }&nbsp; &nbsp; public void setItems(List<ReceiptItem> items) {&nbsp; &nbsp; &nbsp; &nbsp; this.items = items;&nbsp; &nbsp; }&nbsp; &nbsp; public String toFormattedString() {&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder builder = new StringBuilder();&nbsp; &nbsp; &nbsp; &nbsp; builder.append(String.format(RECEIPT_HEADER_FORMAT, getReceiptNumber()));&nbsp; &nbsp; &nbsp; &nbsp; for (ReceiptItem item:getItems()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(item.toFormattedString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return builder.toString();&nbsp; &nbsp; }}Receipt receipt = new Receipt();receipt.addItem(new ReceiptItem("Beef Burgers", new BigDecimal("5.00")));receipt.addItem(new ReceiptItem("Cheese Burgers", new BigDecimal("5.00")));receipt.addItem(new ReceiptItem("Fish and Chips", new BigDecimal("6.00")));receipt.addItem(new ReceiptItem("French Fries", new BigDecimal("4.00")));receipt.addItem(new ReceiptItem("Steak", new BigDecimal("10.00")));receipt.addItem(new ReceiptItem("Sprite Drinks", new BigDecimal("1.00")));receipt.addItem(new ReceiptItem("Soda Drinks", new BigDecimal("0.40")));receipt.addItem(new ReceiptItem("Fuzetea Drinks", new BigDecimal("0.70")));recieptText.setText(reciept.toFormattedString());我对你的第二个问题的回答:JFrame frame = new JFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp;//... Your code hereJButton btnExit = new JButton("Exit");btnExit.setFont(new Font("Tahoma", Font.PLAIN, 19));btnExit.setBounds(766, 484, 127, 39);if (btnExit.isSelected()==true) {&nbsp; &nbsp; System.exit(0);}btnExit.addActionListener((e)-> {&nbsp; &nbsp; int confirm = JOptionPane.showOptionDialog(frame,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Are You Sure to Close this Application?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Exit Confirmation", JOptionPane.YES_NO_OPTION,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.QUESTION_MESSAGE, null, null, null);&nbsp; &nbsp; if (confirm == JOptionPane.YES_OPTION) {&nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; }});frame.getContentPane().add(btnExit);frame.addWindowListener(new WindowAdapter() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void windowClosing(WindowEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; int confirm = JOptionPane.showOptionDialog(frame,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Are You Sure to Close this Application?",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Exit Confirmation", JOptionPane.YES_NO_OPTION,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JOptionPane.QUESTION_MESSAGE, null, null, null);&nbsp; &nbsp; &nbsp; &nbsp; if (confirm == JOptionPane.YES_OPTION) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.exit(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }});frame.setVisible(true);frame.pack();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java