如何从Java中的JTextField检索字符串到Action Listener?

时间:2017-06-14 03:47:10

标签: java swing actionlistener jtextfield

我正在使用Java Ready 1.4。 我已经提到为从JTextField获取值而发布的代码并尝试了它们,但是我得到了这个compliation错误: 没有名为" getText"在类型" java.lang.String。"

中找到

我的节目:

import java.awt.*; //for Dimension
import javax.swing.*; //for GUI components
import java.awt.event.*; //for MessageListener

public class MortgageCalculator extends JFrame implements ActionListener
{
public String nameField, ageField, incomeField, priceField, downPayField, interestRateField, paymentField, amortField;

public MortgageCalculator ()
{
    //Frame
    JFrame frame = new JFrame ();
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.setSize (new Dimension (300, 250));
    frame.setTitle ("Mortgage Calculator");
    frame.getContentPane ().setLayout (new BorderLayout ());

    //User Information (Input) - Center Panel
    JPanel centerPanel = new JPanel (new GridLayout (16, 1));
    //Personal Information
    centerPanel.add (new JLabel ("Personal Information", SwingConstants.CENTER));
    centerPanel.add (new JLabel ("Full Name:"));
    final JTextField nameField = new JTextField ();
    centerPanel.add (nameField);
    centerPanel.add (new JLabel ("Your Age:"));
    final JTextField ageField = new JTextField ();
    centerPanel.add (ageField);
    centerPanel.add (new JLabel ("Yearly Income:"));
    final JTextField incomeField = new JTextField ();
    centerPanel.add (incomeField);
    centerPanel.add (new JLabel ());
    //Mortgage Information
    centerPanel.add (new JLabel ("Mortgage Information", SwingConstants.CENTER));
    centerPanel.add (new JLabel ("Price of Property:"));
    final JTextField priceField = new JTextField ();
    centerPanel.add (priceField);
    centerPanel.add (new JLabel ("Down Payment:"));
    final JTextField downPayField = new JTextField ();
    centerPanel.add (downPayField);
    centerPanel.add (new JLabel ("Interest Rate (%):"));
    final JTextField interestRateField = new JTextField ();
    centerPanel.add (interestRateField);
    centerPanel.add (new JLabel ("Amortization Period (Years):"));
    frame.getContentPane ().add (centerPanel, BorderLayout.CENTER);

    //User Options (Input) - South Panel
    JPanel southPanel = new JPanel (new GridLayout (8, 3));
    //Payment Option
    southPanel.add (new JLabel ("Payment:"));
    southPanel.add (new JLabel ("Type in 1, 2 or 3"));
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ("1. Weekly"));
    southPanel.add (new JLabel ("2. Biweekly"));
    southPanel.add (new JLabel ("3. Monthly"));
    final JTextField paymentField = new JTextField ();
    southPanel.add (paymentField);
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());

    //Amortization Period Option
    southPanel.add (new JLabel ("Amortization Period:"));
    southPanel.add (new JLabel ("Type in 1, 2 or 3"));
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ("1. 20 years"));
    southPanel.add (new JLabel ("2. 25 years"));
    southPanel.add (new JLabel ("3. 30 years"));
    final JTextField amortField = new JTextField ();
    southPanel.add (amortField);
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    //To add a blank line in frame, added three blank JLabels because it is 3 horiontal for grid layout
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    southPanel.add (new JButton ("Clear Information"));
    southPanel.add (new JLabel ());
    JButton nextButton = new JButton ("Next");
    southPanel.add (nextButton);

    frame.getContentPane ().add (southPanel, BorderLayout.SOUTH);


    frame.pack ();
    frame.setVisible (true);


    nextButton.addActionListener (this);

}


public static void main (String[] args)
{
    MortgageCalculator mainApp = new MortgageCalculator ();
}


public void actionPerformed (ActionEvent e)
{
    //Gets the personal information from the text fields
    String name;
    int age, payment, amort;
    double income, price, downPay, interestRate;

    name = nameField.getText ();
    age = Integer.parseInt (ageField.getText ());
    income = Double.parseDouble (incomeField.getText ());
    price = Double.parseDouble (priceField.getText ());
    downPay = Double.parseDouble (downPayField.getText ());
    interestRate = Double.parseDouble (interestRateField.getText ());
    payment = Integer.parseInt (paymentField.getText ());
    amort = Integer.parseInt (amortField.getText ());

    interestRate = interestRate / 100;


    //Output frame
    JFrame frame2 = new JFrame ();
    frame2.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame2.setSize (new Dimension (300, 250));
    frame2.setTitle ("Mortgage Calculator");
    frame2.getContentPane ().setLayout (new BorderLayout ());

    JPanel westPanel2 = new JPanel (new GridLayout (3, 1));
    JLabel nameLabel = new JLabel ("Name: " + name);
    westPanel2.add (nameLabel);
    JLabel ageLabel = new JLabel ("Age: " + age);
    westPanel2.add (ageLabel);
    JLabel incomeLabel = new JLabel ("Yearly Income: $" + income);
    westPanel2.add (incomeLabel);
    frame2.getContentPane ().add (westPanel2, BorderLayout.WEST);

    JPanel centerPanel2 = new JPanel (new GridLayout (3, 1));
    JLabel priceLabel = new JLabel ("    Price of property: $" + price);
    centerPanel2.add (priceLabel);
    JLabel downPayLabel = new JLabel ("    Down Payment: $" + downPay);
    centerPanel2.add (downPayLabel);
    JLabel interestRateLabel = new JLabel ("    Interest Rate: " + income);
    centerPanel2.add (interestRateLabel);
    frame2.getContentPane ().add (centerPanel2, BorderLayout.CENTER);


    double interest, numerator, l, denominator, payPerPeriod, initialP, mortgageYearly;
    if (payment == 1)
    {
        interest = 1 + interestRate / 52;
        if (amort == 1)
        {
            l = Math.pow (interest, 20 * 52);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        else if (amort == 2)
        {
            l = Math.pow (interest, 25 * 52);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        else // 3. amort30
        {
            l = Math.pow (interest, 30 * 52);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        mortgageYearly = payPerPeriod * 52;
    }
    else if (payment == 2)
    {
        interest = 1 + interestRate / 26;
        if (amort == 1)
        {
            l = Math.pow (interest, 20 * 26);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        else if (amort == 2)
        {
            l = Math.pow (interest, 25 * 26);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        else // 3. amort30
        {
            l = Math.pow (interest, 30 * 26);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        mortgageYearly = payPerPeriod * 26;


    }
    else //3. monthly payment
    {
        interest = 1 + interestRate / 12;
        if (amort == 1)
        {
            l = Math.pow (interest, 20 * 12);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        else if (amort == 2)
        {
            l = Math.pow (interest, 25 * 12);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;
        }
        else // 2. amort30
        {
            l = Math.pow (interest, 30 * 12);
            numerator = l * interest;
            denominator = l - 1;
            initialP = price - downPay;
            payPerPeriod = (numerator / denominator) * initialP;

        }
        mortgageYearly = payPerPeriod * 12;
    }


    JPanel eastPanel2 = new JPanel (new GridLayout (3, 1));
    JLabel payTotalLabel = new JLabel ("    Payment Total: $" + initialP);
    eastPanel2.add (priceLabel);
    JLabel payPerPeriodLabel = new JLabel ("    Payment per period: $" + payPerPeriod);
    eastPanel2.add (payPerPeriodLabel);

    frame2.getContentPane ().add (eastPanel2, BorderLayout.EAST);


    JPanel southPanel2 = new JPanel (new GridLayout (2, 1));
    double minimumIncome = mortgageYearly / 0.28;
    JLabel minimumLabel = new JLabel ("    Minimum Yearly Income: $" + minimumIncome);
    eastPanel2.add (priceLabel);

    if (minimumIncome > income)
    {
        JLabel qualifiedLabel = new JLabel ("You are not qualified for the mortgage");
        eastPanel2.add (qualifiedLabel);
    }
    else
    {
        JLabel qualifiedLabel = new JLabel ("You are qualified for the mortgage");
        eastPanel2.add (qualifiedLabel);
    }


    frame2.getContentPane ().add (eastPanel2, BorderLayout.EAST);



    frame2.pack ();
    frame2.setVisible (true);



}

}

1 个答案:

答案 0 :(得分:0)

这个错误很简单,但我可以看出它为什么会让你失望。基本上,您的所有field变量(例如nameFieldageField)都定义为班级顶部的String。更改此行中的String

public String nameField, ageField, incomeField, priceField, downPayField, interestRateField, paymentField, amortField;

JTextField。然后,在main方法内部,而不是像这样定义每个字段:     final JTextField incomeField = new JTextField();

请改为:incomeField = new JTextField ();