简体   繁体   English

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

[英]How do I get information of the variables from one class to another Action Listener class?

This is my code for the two file classes. 这是我对两个文件类的代码。 What do I have to add/fix in order for the variables to transfer and print in the action listener JFrame? 为了使变量在动作侦听器JFrame中传输和打印,我必须添加/修复什么? The error "No field named "(the variable I used in action listener from main class)" was found in type "Next" 在类型“下一步”中找到错误“没有名为“(我在主类的操作侦听器中使用的变量)的字段”

  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);

 }
}

The ActionListener: 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);

  }
}

The variables in your main method are local variables, so your listener won't be able to see them. main方法中的变量是局部变量,因此您的侦听器将无法看到它们。

There are a lot of ways to fix this. 有很多方法可以解决此问题。 First, you have to make the state variables ( name , etc) member variables of the MortgageCalculator class, so that they'll be visible to other methods or classes. 首先,必须使MortgageCalculator类的状态变量( name等)成为成员变量,以便其他方法或类可以看到它们。 Then, give the listener code access to those variables. 然后,给侦听器代码访问这些变量的权限。 Some options: 一些选项:

  • Make Next an inner class of MortgageCalculator , so that it can automatically see the member variables. Next设为MortgageCalculator的内部类,以便它可以自动查看成员变量。
  • Make MortgageCalculator implement ActionListener , and move the actionPerformed method inside the resulting class. 使MortgageCalculator实现ActionListener ,并将actionPerformed方法移动到结果类中。 Then, instead of writing nextButton.addActionListener(new Next()); 然后,而不是编写nextButton.addActionListener(new Next()); , you'd say nextButton.addActionListener(this); ,您会说nextButton.addActionListener(this); - but only inside a non-static method (so not main - you'd need to make main call a non-static method on an instance of MortgageCalculator ). -但仅在非静态方法内(所以不是main您需要在MortgageCalculator实例上使main调用非静态方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用来自另一个类的Action Listener的目录路径变量? - How do I use a directory path variable from an Action Listener from one class in another? 如何将信息从一个 class 传递到另一个 - how do I pass information from one class to another 如何将变量从一个类传递到另一个类以在包装器中使用 - How do I get variables from one class to another to be used in a wrapper 如何在另一个班级的活动中设置侦听器? - How do i set a listener in an activity from another class? 如何从另一个类中的Java类调用变量 - How do I call variables from a Java class in another Class 如何将变量从Java类调用到另一个类? - How do I call variables from a Java class into another Class? 如何将变量/对象从动作侦听器传递到驱动程序类? - How to pass variables/objects from action listener to driver class? 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class? 如何检测是否已从另一个 class 触发动作侦听器? - How to detect if a action listener has been triggered from another class? 如何使用JButton从另一个类打开JFrame。 目前,我使用链接到动作监听器的按钮 - how can i open a JFrame from another class with a JButton . For the moment i use a button linked to action listener
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM