gpt4 book ai didi

带有复选框的 Java 嵌套 JPanel

转载 作者:行者123 更新时间:2023-12-01 11:47:50 24 4
gpt4 key购买 nike

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

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

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

这是在我的 ClickActionactionPerformed 方法中:

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
}

最佳答案

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

如果您希望它显示为弹出窗口而不是在主窗口中,您可以将deliveryPanel 添加到按下“order”事件后显示的JOptionPane。

例如:

JOptionPane.showMessageDialog(null,deliveryPanel,"Order",JOptionPane.INFORMATION_MESSAGE);

关于带有复选框的 Java 嵌套 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017340/

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