在139-146行中,出现错误“无法将Double转换为JTextField”。我知道我需要更改变量的名称,但是不确定在哪里更改。我试图更改CalcButtonListener下的名称,但它创建了更多错误。感谢您的时间和帮助!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TravelExpenses extends JFrame
{
    private JPanel panel;
    private JPanel buttonPanel;

    private JTextField numDays;
    private JTextField airfare;
    private JTextField carRent;
    private JTextField miles;
    private JTextField parking;
    private JTextField taxi;
    private JTextField reg;
    private JTextField lodge;

    private JButton calcButton;
    private JButton resetButton;


public TravelExpenses()
{
    setTitle("Travel Expenses");

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buildPanel();

    buildButtonPanel();

    add(panel, BorderLayout.CENTER);
    add(buttonPanel, BorderLayout.SOUTH);

    pack();
    setVisible(true);
}

private void buildPanel()
{
    //labels for text fields
    JLabel numDaysLabel = new JLabel("Number of days of the trip:");
    JLabel airfareLabel = new JLabel("Amount of airfare:");
    JLabel carRentLabel = new JLabel("Amount of car rental:");
    JLabel milesLabel = new JLabel("Miles driven(if a private vehicle was used):");
    JLabel parkingLabel = new JLabel("Parking fees:");
    JLabel taxiLabel = new JLabel("Taxi fees:");
    JLabel regLabel = new JLabel("Conference registaration:");
    JLabel lodgeLabel = new JLabel("Lodging charges per night:");

    //text fields
     numDays = new JTextField(10);
     airfare = new JTextField(10);
     carRent = new JTextField(10);
     miles = new JTextField(10);
     parking = new JTextField(10);
     taxi = new JTextField(10);
     reg = new JTextField(10);
     lodge = new JTextField(10);

    //new panel
    panel = new JPanel();

    //layout manager
    panel.setLayout(new GridLayout( 10,8));

    //add labels and text fields
    panel.add(numDaysLabel);
    panel.add(numDays);
    panel.add(airfareLabel);
    panel.add(airfare);
    panel.add(carRentLabel);
    panel.add(carRent);
    panel.add(milesLabel);
    panel.add(miles);
    panel.add(parkingLabel);
    panel.add(parking);
    panel.add(taxiLabel);
    panel.add(taxi);
    panel.add(regLabel);
    panel.add(reg);
    panel.add(lodgeLabel);
    panel.add(lodge);

    //put border around panel
    panel.setBorder(BorderFactory.createEmptyBorder(10,10, 1, 10));

}

//method creates button panel
private void buildButtonPanel()
{
    //create button for calc
    calcButton = new CButton("Calculate");

    calcButton.addActionListener(new CalcButtonListener());

    resetButton = new JButton("Reset");

    resetButton.addActionListener(new ResetButtonListener());

    buttonPanel = new JPanel();
    buttonPanel.add(resetButton);
    buttonPanel.add(calcButton);
}
//listener for calc button
private class CalcButtonListener implements ActionListener
{
    double numDays;
    double airfare;
    double carRent;
    double miles;
    double parking;
    double taxi;
    double reg;
    double lodge;

    public void actionPerformed(Action e)
    {
        double total;
        String msg;

        //get data
        getData();

        //total
        total = determineTotal();

        msg = String.format("Total cost: $%,.2f\n", total);

        JOptionPane.showMessageDialog(null, msg);

    }
}

**Below is the chunk I am having issues with**
private void getData()
{
    numDays = Double.parseDouble(numDays.getText());
    airfare = Double.parseDouble(airfare.getText());
    carRent = Double.parseDouble(carRent.getText());
    miles = Double.parseDouble(miles.getText());
    parking = Double.parseDouble(parking.getText());
    taxi = Double.parseDouble(taxi.getText());
    reg = Double.parseDouble(reg.getText());
    lodge = Double.parseDouble(lodge.getText());
}



最佳答案

在139-146行中,您正在访问TravelExpenses类的成员,而不是CalcButtonListener类。

将getData()和defineTotal()方法放在CalcButtonListener类中,并使用特定成员的作用域说明符,例如JtextField use:TravelExpenses.this.numDays,就像所有JtextField一样。

对CalcButtonListener使用以下代码:

private class CalcButtonListener implements ActionListener
{
double numDays;
double airfare;
double carRent;
double miles;
double parking;
double taxi;
double reg;
double lodge;

public void actionPerformed(Action e)
{
    double total;
    String msg;

    //get data
    getData();

    //total
    total = determineTotal();

    msg = String.format("Total cost: $%,.2f\n", total);

    JOptionPane.showMessageDialog(null, msg);

}
private void getData()
{
    numDays = Double.parseDouble(TravelExpenses.this.numDays.getText());
    airfare = Double.parseDouble(TravelExpenses.this.airfare.getText());
    carRent = Double.parseDouble(TravelExpenses.this.carRent.getText());
    miles = Double.parseDouble(TravelExpenses.this.miles.getText());
    parking = Double.parseDouble(TravelExpenses.this.parking.getText());
    taxi = Double.parseDouble(TravelExpenses.this.taxi.getText());
    reg = Double.parseDouble(TravelExpenses.this.reg.getText());
    lodge = Double.parseDouble(TravelExpenses.this.lodge.getText());

}

private double determineTotal()
{
    double total = numDays * airfare * carRent * miles * parking * taxi * reg * lodge;

    return total;
}
}


在这里您可以找到完整的代码。

    import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class TravelExpenses extends JFrame {
    private JPanel panel;
    private JPanel buttonPanel;

    private JTextField numDays;
    private JTextField airfare;
    private JTextField carRent;
    private JTextField miles;
    private JTextField parking;
    private JTextField taxi;
    private JTextField reg;
    private JTextField lodge;

    private JButton calcButton;
    private JButton resetButton;

    public TravelExpenses() {
        setTitle("Travel Expenses");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        buildPanel();

        buildButtonPanel();

        add(panel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

    private void buildPanel() {
        // labels for text fields
        JLabel numDaysLabel = new JLabel("Number of days of the trip:");
        JLabel airfareLabel = new JLabel("Amount of airfare:");
        JLabel carRentLabel = new JLabel("Amount of car rental:");
        JLabel milesLabel = new JLabel("Miles driven(if a private vehicle was used):");
        JLabel parkingLabel = new JLabel("Parking fees:");
        JLabel taxiLabel = new JLabel("Taxi fees:");
        JLabel regLabel = new JLabel("Conference registaration:");
        JLabel lodgeLabel = new JLabel("Lodging charges per night:");

        // text fields
        numDays = new JTextField(10);
        airfare = new JTextField(10);
        carRent = new JTextField(10);
        miles = new JTextField(10);
        parking = new JTextField(10);
        taxi = new JTextField(10);
        reg = new JTextField(10);
        lodge = new JTextField(10);

        // new panel
        panel = new JPanel();

        // layout manager
        panel.setLayout(new GridLayout(10, 8));

        // add labels and text fields
        panel.add(numDaysLabel);
        panel.add(numDays);
        panel.add(airfareLabel);
        panel.add(airfare);
        panel.add(carRentLabel);
        panel.add(carRent);
        panel.add(milesLabel);
        panel.add(miles);
        panel.add(parkingLabel);
        panel.add(parking);
        panel.add(taxiLabel);
        panel.add(taxi);
        panel.add(regLabel);
        panel.add(reg);
        panel.add(lodgeLabel);
        panel.add(lodge);

        // put border around panel
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 1, 10));

    }

//method creates button panel
    private void buildButtonPanel() {
        // create button for calc
        calcButton = new CButton("Calculate");

        calcButton.addActionListener(new CalcButtonListener());

        resetButton = new JButton("Reset");

        resetButton.addActionListener(new ResetButtonListener());

        buttonPanel = new JPanel();
        buttonPanel.add(resetButton);
        buttonPanel.add(calcButton);
    }

//listener for calc button
    private class CalcButtonListener implements ActionListener {
        double numDays;
        double airfare;
        double carRent;
        double miles;
        double parking;
        double taxi;
        double reg;
        double lodge;

        public void actionPerformed(ActionEvent e) {
            double total;
            String msg;

            // get data
            getData();

            // total
            total = determineTotal();

            msg = String.format("Total cost: $%,.2f\n", total);

            JOptionPane.showMessageDialog(null, msg);

        }

        private void getData() {
            numDays = Double.parseDouble(TravelExpenses.this.numDays.getText());
            airfare = Double.parseDouble(TravelExpenses.this.airfare.getText());
            carRent = Double.parseDouble(TravelExpenses.this.carRent.getText());
            miles = Double.parseDouble(TravelExpenses.this.miles.getText());
            parking = Double.parseDouble(TravelExpenses.this.parking.getText());
            taxi = Double.parseDouble(TravelExpenses.this.taxi.getText());
            reg = Double.parseDouble(TravelExpenses.this.reg.getText());
            lodge = Double.parseDouble(TravelExpenses.this.lodge.getText());

        }

        private double determineTotal() {
            double total = numDays * airfare * carRent * miles * parking * taxi * reg * lodge;

            return total;
        }
    }

    private class ResetButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // reset text fields
            numDays.setText("0");
            airfare.setText("0");
            carRent.setText("0");
            miles.setText("0");
            parking.setText("0");
            taxi.setText("0");
            reg.setText("0");
            lodge.setText("0");

        }
    }

    public static void main(String[] args) {
        TravelExpenses gc = new TravelExpenses();
    }
}

08-06 17:00