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

Java 无法运行string.format+;退出按钮有问题,java,Java,我正在做我的第一个项目。当我试图转到字符串.format中的新行时,不知何故失败了(要么根本无法打印,要么在同一行中打印) 代码如下: String reciept = String.format("Recipt number #16424 +" + "%n" + "Beef Burgers :" + ab1 +"%n" + "Cheese Burgers :" + ab2 + "%n" + "Fish and Chips :" + ab3 + "%n" + "French Fr

我正在做我的第一个项目。当我试图转到
字符串.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);
还有一个问题。我正在尝试创建
JButton
exit。我试图打开一条消息,上面写着如果我确定退出,然后退出,但我失败了,所以我尝试了reg one,如果他按下退出按钮,它将退出,我也失败了

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

斯诺,你正处在一个漫长但有趣且富有成果的旅程的开始。祝你好运 对于第一个问题,在面向对象的设计原则方面,在不同的完善程度上有许多正确的答案。我需要建议您尽早阅读Java编码标准、控制反转和单元测试。我将只提到封装,并向您推荐以下解决方案

我首先创建两个类Receipt和ReceiptItem(可以根据项目的范围创建Product、ProductPrice等),假设您要打印的不仅仅是一个收据(可能为了存储收据以备日后检查,您还需要一个持久层,这是另一个需要探索的字段),但您将使用更多的收据明细行处理许多收据。然后,我将创建这两个类的实例,并在将其发送到文本显示/打印设备之前调用Receipt类的formattedString方法

public class ReceiptItem {
    private static final int TITLE_WIDTH = 30;
    private static final String DETAIL_FORMAT="%-" + TITLE_WIDTH + "s : %6s%n";

    private String title;
    private BigDecimal totalPrice;

    public ReceiptItem(String title, BigDecimal totalPrice) {
        setTitle(title);
        setTotalPrice(totalPrice);
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public BigDecimal getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(BigDecimal totalPrice) {
        this.totalPrice = totalPrice;
    }

    public String toFormattedString() {
        return String.format(DETAIL_FORMAT, getTitle(),
                getTotalPrice().toPlainString());
    }
}


public class Receipt {
    private static final String RECEIPT_HEADER_FORMAT = "Receipt number #%s%n";
    private static int LAST_RECEIPT_NUMBER = 0;
    private int receiptNumber;
    private List<ReceiptItem> items= new ArrayList<>();

    public Receipt() {
        super();
        setReceiptNumber(++LAST_RECEIPT_NUMBER);
    }

    public Receipt(int receiptNumber) {
        this();
        setReceiptNumber(LAST_RECEIPT_NUMBER=receiptNumber);
    }

    public void addItem(ReceiptItem item) {
        items.add(item);
    }

    public int getReceiptNumber() {
        return receiptNumber;
    }
    public void setReceiptNumber(int receiptNumber) {
        this.receiptNumber = receiptNumber;
    }
    public List<ReceiptItem> getItems() {
        return items;
    }
    public void setItems(List<ReceiptItem> items) {
        this.items = items;
    }
    public String toFormattedString() {
        StringBuilder builder = new StringBuilder();
        builder.append(String.format(RECEIPT_HEADER_FORMAT, getReceiptNumber()));
        for (ReceiptItem item:getItems()) {
            builder.append(item.toFormattedString());
        }
        return builder.toString();
    }
}


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

你应该在像tutorialspoint这样的好网站上开始学习java。嗯,我尽最大努力在youtube上观看视频,我更喜欢做而不是阅读,你知道答案吗?
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
//... Your code here
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);
}

btnExit.addActionListener((e)-> {
    int confirm = JOptionPane.showOptionDialog(frame,
            "Are You Sure to Close this Application?",
            "Exit Confirmation", JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE, null, null, null);
    if (confirm == JOptionPane.YES_OPTION) {
        System.exit(0);
    }
});

frame.getContentPane().add(btnExit);
frame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        int confirm = JOptionPane.showOptionDialog(frame,
                "Are You Sure to Close this Application?",
                "Exit Confirmation", JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE, null, null, null);
        if (confirm == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }
});
frame.setVisible(true);
frame.pack();