【发布时间】:2015-09-25 06:25:43
【问题描述】:
我不确定为什么这不起作用,我尽我所能。我觉得我非常接近做对了,但只是不能把最后几件事放在一起。我也意识到这不是很好的编码,但我是这方面的初学者,如果我能得到任何帮助,我将不胜感激。
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
Object flight = AirlinesBox.getSelectedItem();
Object DepartureLocation = LocationsBox.getSelectedItem();
Object Destination = DestinationBox.getSelectedItem();
Object ReturnOrSingle = TypeOfFlightBox.getSelectedItem();
Object FlightDate = DateBox.getSelectedItem();
Object FlightTime = TimeBox.getSelectedItem();
Object FlightSeat = classOfSeatBox.getSelectedItem();
Object FlightBag = FlightBagBox.getSelectedItem();
Object FlightMeal = FlightMealBox.getSelectedItem();
if(source == PrintTicket)
{
try {
PrintWriter write = new PrintWriter("Receipt.txt");
write.println("_______________________________________");
write.println("| |");
write.println("| -------------------------------- |");
write.println("| | This is your Flight Receipt | |");
write.println("| | Please keep it safe | |");
write.println("| -------------------------------- |");
write.println("| |");
write.println("| -------------------------------- |");
write.println("| | | |");
write.println(" Airline= " + flight);
write.println(" Departure Location= " + DepartureLocation);
write.println(" Destination= " + Destination);
write.println(" Flight Type= " + ReturnOrSingle);
write.println(" Flight Day= " + FlightDate);
write.println(" Flight Time= " + FlightTime);
write.println(" Flight Seat Type= " + FlightSeat);
write.println(" Extra Bags= " + FlightBag);
write.println(" Flight Meal= " + FlightMeal);
write.println("| | | |");
write.println("| -------------------------------- |");
write.println("----------------------------------------");
write.close();
System.out.println(AirlinesBox.getSelectedItem());
System.out.println(LocationsBox.getSelectedItem());
System.out.println(DestinationBox.getSelectedItem());
System.out.println(TypeOfFlightBox.getSelectedItem());
System.out.println(DateBox.getSelectedItem());
System.out.println(TimeBox.getSelectedItem());
System.out.println(classOfSeatBox.getSelectedItem());
System.out.println(FlightBagBox.getSelectedItem());
System.out.println(FlightMealBox.getSelectedItem());
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
【问题讨论】:
-
是时候进行一些调试了,因为您必须先隔离错误,然后才能修复它。
-
这是最小的?
-
您是否将此
ActionListener添加到PrintTicket组件中?也许source == PrintTicket永远不是true -
"this is the minimal"-- 完全不可运行且不可测试。请阅读或重新阅读第一条评论的链接。此外,"thats is the problem there is not errors when its running it simply doesnt do anything i want it to print the receipt into a new txt file but when the button is pressed it doesnt do anything, i only put the println part in there to make sure that the If statement was working"-- 调试不仅仅是修复编译错误。这意味着使用调试器并在运行时跟踪代码以查看逻辑错误在哪里。
标签: java swing jcombobox printwriter