如何从一个类到另一个Action Listener类获取变量的信息?

时间:2017-06-06 02:54:08

标签: java swing actionlistener

这是我的两个文件类的代码。我需要添加/修复哪些变量才能在动作侦听器JFrame中传输和打印?在“Next”类型中找到错误“No field named”(我在主类的动作监听器中使用的变量)“

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

  public class MortgageCalculator
  {
   public static void main (String[] args)
    {
    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 ());

    frame.getContentPane ().add (new JLabel ("                          Personal Information"), BorderLayout.NORTH);

    JPanel centerPanel = new JPanel (new GridLayout (15, 1));
    centerPanel.add (new JLabel ("Full Name:"));
    JTextField nameField = new JTextField ();
    centerPanel.add (nameField);
    centerPanel.add (new JLabel ("Your Age:"));
    JTextField ageField = new JTextField ();
    centerPanel.add (ageField);
    centerPanel.add (new JLabel ("Yearly Income:"));
    JTextField incomeField = new JTextField ();
    centerPanel.add (incomeField);
    centerPanel.add (new JLabel ());
    centerPanel.add (new JLabel ("                          Mortgage Information"));

    centerPanel.add (new JLabel ("Price of Property:"));
    JTextField priceField = new JTextField ();
    centerPanel.add (priceField);
    centerPanel.add (new JLabel ("Down Payment:"));
    JTextField downPayField = new JTextField ();
    centerPanel.add (downPayField);
    centerPanel.add (new JLabel ("Interest Rate:"));
    JTextField interestRateField = new JTextField ();
    centerPanel.add (interestRateField);
    centerPanel.add (new JLabel ("Amortization Period (Years):"));
    frame.getContentPane ().add (centerPanel, BorderLayout.CENTER);

    JPanel southPanel = new JPanel (new GridLayout (5, 2));
    JButton amort20 = new JButton ("20");
    southPanel.add (amort20);
    JButton amort25 = new JButton ("25");
    southPanel.add (amort25);
    JButton amort30 = new JButton ("30");
    southPanel.add (amort30);
    southPanel.add (new JLabel ("Payment Option:"));
    southPanel.add (new JLabel ());
    southPanel.add (new JLabel ());
    JButton paymentWeek = new JButton ("Weekly");
    southPanel.add (paymentWeek);
    JButton paymentBiweek = new JButton ("Biweekly");
    southPanel.add (paymentBiweek);
    JButton paymentMonth = new JButton ("Monthly");
    southPanel.add (paymentMonth);
    //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 (new Next ());

    //Gets the personal information from the text fields
    int age, income, price, downPay, interestRate;
    String name = nameField.getText ();
    String ageText = ageField.getText ();
    age = Integer.parseInt (ageText);
    String incomeText = incomeField.getText ();
    income = Integer.parseInt (ageText);
    //Gets the mortgage information from the text fields
    String priceText = priceField.getText ();
    price = Integer.parseInt (ageText);
    String downPayText = downPayField.getText ();
    downPay = Integer.parseInt (ageText);
    String interestRateText = interestRateField.getText ();
    interestRate = Integer.parseInt (interestRateText);

 }
}

ActionListener:

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

public class Next implements ActionListener
{
 public void actionPerformed (ActionEvent event)
 {
    //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);

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

  }
}

1 个答案:

答案 0 :(得分:1)

main方法中的变量是局部变量,因此您的听众无法看到它们。

有很多方法可以解决这个问题。首先,您必须创建name类的状态变量(MortgageCalculator等)成员变量,以便它们对其他方法或类可见。然后,为侦听器代码提供对这些变量的访问权限。一些选择:

  • 使Next成为MortgageCalculator的内部类,以便它可以自动查看成员变量。
  • 使MortgageCalculator实现ActionListener,并在生成的类中移动actionPerformed方法。然后,不是写nextButton.addActionListener(new Next());,而是说nextButton.addActionListener(this); - 而只是在非静态方法中(所以不是main - 你需要制作main 1}}在MortgageCalculator)的实例上调用非静态方法。