有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从文本文件读取标签

所以我有一个程序,需要保存到文件中,然后从中读取,我已经从文本文件中读取了,但我无法从文件中读取,我以前有过静态的,但老师不喜欢,让我将其更改为从文件中读取。我以前从未使用过gui,他在gui中向我们展示的唯一内容就是如何放置按钮和文本字段

private DefaultListModel<String> populateFlights() { // populate avalible flights to the list

        DefaultListModel<String> list = new DefaultListModel<String>();
        ArrayList<Flight> FlightList = MainMenu.getAirlineMgr().getFlightList();

        // loop to get flight list
        for (int i = 0; i < FlightList.size(); i++) {
            list.addElement(FlightList.get(i).getFlightNumber());
        }

        return list;
    }

    private void PopulateAvailableSeats(Flight flight, JComboBox<String> cb) { 
        Seat lSeat;
        cb.removeAllItems();
        for (int i = 0; i < flight.getSeats().size(); i++) {
            lSeat = flight.getSeats().get(i);
            if (lSeat.getSeatstatus() == Seat.Status.AVAILABLE) {
                cb.addItem(lSeat.getSeatNo());
            }
        }
    }

    private Double CalculateTotalCost(Flight flight, int discount, Boolean suitCase) {
        Double total;
        total = flight.getCost() - ((discount * flight.getCost()) / 100); // discount 


        if (suitCase) { // if statement to add suitcase if applyed
            total = total + 50;
        }
        return total;
    }

    private void ConfirmBooking(Flight flight, Passenger passenger, String seatNumber, Boolean suitCase) {
        Double totalCost = CalculateTotalCost(flight, passenger.getDiscount(), suitCase);
        Seat lSeat = flight.getSeatByNumber(seatNumber);

        // Assign the Passenger to Seat and change StatusSeat to RESERVED/booked
        passenger.setSeatNo(lSeat.getSeatNo());
        lSeat.setPassenger(passenger);
        lSeat.setSeatstatus(Seat.Status.RESERVED);

        // Create a booking
        Book booking = new Book(passenger);
        booking.setFlightToBook(flight);
        booking.setSeat(lSeat);
        booking.setSuitCase(suitCase);
        booking.setTotalCost(totalCost);

        // Assign booking to the passenger
        passenger.setReservation(booking);

        // Displays save dialog
        try {
            booking.SaveTicket();
        } catch (IOException ioe) {
            JOptionPane.showMessageDialog(null, "File Error");
        }

    }

    private Boolean ValidateBooking(Flight flight) {
        return (flight.getFlightStatus() == Flight.Status.AVAILABLE)
                || (flight.getFlightStatus() == Flight.Status.CHECKING);
    }

    public BookPanel() {
        setBackground(new Color(176, 224, 230));
        setLayout(null);

        JLabel label = new JLabel("Departure Date:");
        label.setBounds(10, 58, 112, 14);
        add(label);

        JLabel lblSelectAFlight = new JLabel("Select a flight:");
        lblSelectAFlight.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblSelectAFlight.setBounds(10, 306, 121, 14);
        add(lblSelectAFlight);

        JLabel label_2 = new JLabel("Departure Airport:");
        label_2.setBounds(10, 9, 112, 14);
        add(label_2);

        JLabel label_3 = new JLabel("Arrival Airport:");
        label_3.setBounds(218, 9, 112, 14);
        add(label_3);

        JLabel label_4 = new JLabel("Arrival Date:");
        label_4.setBounds(218, 58, 112, 14);
        add(label_4);

        JLabel lblFlightStatus1 = new JLabel("Flight Status:");
        lblFlightStatus1.setBounds(10, 108, 99, 14);
        add(lblFlightStatus1);

        JLabel lbDepartureAirport = new JLabel("");
        lbDepartureAirport.setForeground(Color.GREEN);
        lbDepartureAirport.setBackground(Color.ORANGE);
        lbDepartureAirport.setBounds(10, 33, 121, 14);
        add(lbDepartureAirport);

        JLabel lbDepartureDate = new JLabel("");
        lbDepartureDate.setForeground(Color.GREEN);
        lbDepartureDate.setBackground(Color.ORANGE);
        lbDepartureDate.setBounds(10, 83, 183, 14);
        add(lbDepartureDate);

        JLabel lbArrivalAirport = new JLabel("");
        lbArrivalAirport.setForeground(Color.MAGENTA);
        lbArrivalAirport.setBackground(Color.ORANGE);
        lbArrivalAirport.setBounds(218, 34, 121, 13);
        add(lbArrivalAirport);

        JLabel lbArrivalDate = new JLabel("");
        lbArrivalDate.setForeground(Color.MAGENTA);
        lbArrivalDate.setBackground(Color.ORANGE);
        lbArrivalDate.setBounds(218, 83, 183, 14);
        add(lbArrivalDate);

        JLabel lblFlightStatus = new JLabel("");
        lblFlightStatus
                .setFont(lblFlightStatus.getFont().deriveFont(lblFlightStatus.getFont().getStyle() | Font.BOLD, 13f));
        lblFlightStatus.setForeground(Color.BLACK);
        lblFlightStatus.setBackground(Color.ORANGE);
        lblFlightStatus.setBounds(10, 133, 99, 23);
        add(lblFlightStatus);

        JLabel lblPassengerInfo = new JLabel("Passenger info:");
        lblPassengerInfo.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblPassengerInfo.setBounds(10, 167, 99, 14);
        add(lblPassengerInfo);

        JLabel lblName = new JLabel("Name:");
        lblName.setBounds(10, 192, 69, 14);
        add(lblName);

        JLabel lbPassengerName = new JLabel("");
        lbPassengerName.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbPassengerName.setBackground(Color.WHITE);
        lbPassengerName.setBounds(99, 192, 187, 14);
        add(lbPassengerName);

        JLabel lbSureName = new JLabel("");
        lbSureName.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbSureName.setBackground(Color.WHITE);
        lbSureName.setBounds(99, 212, 187, 14);
        add(lbSureName);

        JLabel lblSureName = new JLabel("Sure Name:");
        lblSureName.setBounds(10, 212, 73, 14);
        add(lblSureName);

        JLabel lblDiscount1 = new JLabel("Discount:");
        lblDiscount1.setBounds(342, 182, 59, 14);
        add(lblDiscount1);

        JLabel lblDiscount = new JLabel("");
        lblDiscount.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblDiscount.setBounds(342, 212, 59, 14);
        add(lblDiscount);

        JLabel lblCost = new JLabel("Cost:");
        lblCost.setBounds(342, 133, 37, 14);
        add(lblCost);

        JSeparator separator = new JSeparator();
        separator.setBounds(449, 144, 1, 2);
        add(separator);

        JSeparator separator_1 = new JSeparator();
        separator_1.setOrientation(SwingConstants.VERTICAL);
        separator_1.setBounds(412, 9, 13, 393);
        add(separator_1);

        JLabel lblType = new JLabel("Type:");
        lblType.setBounds(10, 233, 73, 14);
        add(lblType);

        JLabel lbType = new JLabel("");
        lbType.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbType.setBounds(103, 233, 187, 14);
        add(lbType);

        JComboBox<String> cbSeats = new JComboBox<String>();
        cbSeats.setBounds(289, 313, 112, 20);
        add(cbSeats);

        JLabel lblAvailableSeats = new JLabel("Available seats:");
        lblAvailableSeats.setBounds(196, 316, 112, 14);
        add(lblAvailableSeats);

        JLabel lbCost = new JLabel("");
        lbCost.setFont(new Font("Tahoma", Font.BOLD, 12));
        lbCost.setBounds(342, 157, 59, 14);
        add(lbCost);

        JLabel lblTotalCost1 = new JLabel("Total Cost:");
        lblTotalCost1.setBounds(342, 233, 69, 14);
        add(lblTotalCost1);

        JLabel lblTotalCost = new JLabel("");
        lblTotalCost.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblTotalCost.setBounds(328, 258, 73, 23);
        add(lblTotalCost);

        // Action for CheckBox
        JCheckBox chckbxNewCheckBox = new JCheckBox("Suite case (+$50)");
        chckbxNewCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lblTotalCost.setText(Double.toString(
                        CalculateTotalCost(flightSelected, passenger.getDiscount(), chckbxNewCheckBox.isSelected()))
                        + "$");
            }
        });
        chckbxNewCheckBox.setBounds(180, 272, 142, 23);
        add(chckbxNewCheckBox);

        // Action for button cancel
        JButton button = new JButton("Cancel");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                MainMenu.hideBook();
            }
        });
        button.setBounds(10, 379, 89, 23);
        add(button);

        // Action for button confirm
        JButton btnPay = new JButton("Confirm");
        btnPay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (ValidateBooking(flightSelected)) {
                    ConfirmBooking(flightSelected, passenger, cbSeats.getSelectedItem().toString(),
                            chckbxNewCheckBox.isSelected());

                    // JOptionPane.showMessageDialog(null,
                    // directory.getAbsolutePath()));
                    MainMenu.hideBook();
                } else {
                    JOptionPane.showMessageDialog(null, "The flight is not available");
                }
            }
        });
        btnPay.setBounds(312, 379, 89, 23);
        add(btnPay);

        // passenger info
        passenger = MainMenu.getAirlineMgr().getPassenger();
        if (passenger != null) {
            lbPassengerName.setText(passenger.getFname());
            lbSureName.setText(passenger.getSname());
            lblDiscount.setText(Integer.toString(passenger.getDiscount()) + "%");
            if (passenger.getClass() == Business.class) {
                lbType.setText("BUSINESS");
            } else {
                if (passenger.getClass() == Ordinary.class) {
                    lbType.setText("ORDINARY");
                } else {
                    if (passenger.getClass() == Island.class) {
                        lbType.setText("ISLAND");
                    }
                }
            }
        }

        // flight info
        JList list = new JList(populateFlights());
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                flightSelected = MainMenu.getAirlineMgr().getFlightByNumber(list.getSelectedValue().toString());
                lbDepartureAirport.setText(flightSelected.getDepartureAirport());
                lbArrivalAirport.setText(flightSelected.getArrivalAirport());
                lbDepartureDate.setText(flightSelected.getDepartureDate().toString());
                lbArrivalDate.setText(flightSelected.getArrivalDate().toString());
                lbCost.setText(Double.toString(flightSelected.getCost()) + "£");

                PopulateAvailableSeats(flightSelected, cbSeats);
                lblTotalCost.setText(Double.toString(
                        CalculateTotalCost(flightSelected, passenger.getDiscount(), chckbxNewCheckBox.isSelected()))
                        + "£");

                // if statement for STATUS
                if (flightSelected.getFlightStatus() == Flight.Status.AVAILABLE) {
                    lblFlightStatus.setText("AVAILABLE");
                } else {
                    if (flightSelected.getFlightStatus() == Flight.Status.BOARDING) {
                        lblFlightStatus.setText("BOARDING");
                    } else {
                        if (flightSelected.getFlightStatus() == Flight.Status.CHECKING) {
                            lblFlightStatus.setText("CHECKING");
                        } else {
                            if (flightSelected.getFlightStatus() == Flight.Status.CLOSED) {
                                lblFlightStatus.setText("CLOSED");
                            }
                        }
                    }
                }
            }
        });
        list.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
        list.setBounds(104, 314, 89, 88);
        add(list);

        JLabel lblBookAFlight = new JLabel("Book a Flight");
        lblBookAFlight.setFont(new Font("Tele-Marines", Font.PLAIN, 12));
        lblBookAFlight.setBounds(140, 142, 81, 14);
        add(lblBookAFlight);
    }
}

哦,是的,如果这里的帮助是保存到文件代码:pic of save to file


共 (0) 个答案