0

我正在制作菜单订购表。程序一直运行到最后一个“if 语句”结束,即创建一个带有复选框选项的新面板,然后打开以执行新工作(显示消息或继续订单详细信息)。

我的第一个问题是我的复选框没有被添加到新面板中。我尝试将它包装在一个新的 FlowLayout 中,但只显示标题。

我是 Java 新手,只编程了几个月。这是一个嵌套的 if-else 语句。不知道该怎么办。这是代码的结尾部分。任何指出我可能明显的错误的帮助将不胜感激。我想学习。

这是actionPerformed我的方法里面ClickAction

if (event.getSource()== order) {
    deliveryPanel = new JPanel();
    deliveryPanel.setLayout(new FlowLayout());

    headerLabel.setText("This Order will be for:");
    JCheckBox dv = new JCheckBox("Delivery"); 
    JCheckBox pu = new JCheckBox("Pick Up");
    deliveryPanel.add(dv);
    deliveryPanel.add(pu);

    CheckBoxHandler checkHandler = new CheckBoxHandler();

    pu.addItemListener(checkHandler); 
    dv.addItemListener(checkHandler);


    /*Options for delivery choice*/
    if (JCheckBox == pu) {
        System.out.println("Your Order"+ orderOutput+ "for" +name+ "will be ready for pickup");
    }
    else if (JCheckBox == dv){
        JOptionPane.showInputDialog(null,"Please Enter your Delivery Address");  //input address

        JOptionPane.showMessageDialog(null,"**Order Details**"+"\n"+"Name: "+name+"\n"+"Address: "+
            address+"\n"+"Order: "+orderOutput+"\n");
    }

    nameOutputMsg = "Welcome " + name + ".\n";
    greetingOutputMsg = "\nThank you for visiting Famous Favorite's Subs!" + "\n";

    //create output string
    outputMsg = nameOutputMsg
                + "**Order Details**"
                + "\nOrder: " + orderOutput
                + "\nQuantity : " + quantity
                + "\nDelivery Address: " + address
                + greetingOutputMsg;


    /*
     * output the order information to a file. Finally,
     * you will read this file to display the order confirmation information.
     */
    FileWriter fw = null; 
    try {
        fw = new FileWriter(new File("output.txt"));
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File could not be accessed/Created. ");
        System.exit(1);
    }
    try {
        fw.write(outputMsg);
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File could not be written.");
        System.exit(1);
    }
    try {
        fw.close();
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File could not be saved. ");
        System.exit(1);
    }

    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(new FileReader("output.txt"));
    }
    catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "File could not be read or file not exists. ");
        System.exit(1);
    }
    String fileText="";

    while(fileScanner.hasNext()) {
        fileText += fileScanner.nextLine()+"\n";
    }
    fileScanner.close();
    // display output message
    JOptionPane.showMessageDialog(null, fileText);
    System.exit(0);


}

if (event.getSource() == exit) {
    JOptionPane.showMessageDialog(null, "Thank you for your Business.", "Goodbye", JOptionPane.INFORMATION_MESSAGE);
     System.exit(1); // terminate application
}
4

1 回答 1

1

目前您没有显示您的deliveryPanel,您只是在创建它。您需要将其添加到框架中才能使其可见。

如果您希望它显示为弹出窗口而不是主窗口中,您可以将您的 deliveryPanel 添加到 JOptionPane 中,一旦按下“订单”事件就会显示。

例如:

JOptionPane.showMessageDialog(null,deliveryPanel,"Order",JOptionPane.INFORMATION_MESSAGE);
于 2015-03-12T18:51:37.047 回答