私はメニュー注文フォームに取り組んでいます。プログラムは、最後の "if ステートメント" があるところまで実行されます。これは、チェック ボックス オプションを使用して新しいパネルを作成し、それを開いて新しい作業を実行する (メッセージを表示するか、注文の詳細を続行する) ためです。
最初の問題は、チェック ボックスが新しいパネルに追加されていないことです。新しい FlowLayout でラップしようとしましたが、ヘッダーだけが表示されています。
私はJavaが初めてで、プログラミングを始めて数か月しか経っていません。これはネストされた if-else ステートメントです。何をすべきかわからない。これがコードの最後の部分です。私のおそらく明らかな間違いを指摘していただければ幸いです。学びたい。
これはactionPerformed
my のメソッド内にあります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
}