[英]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:
一些选项:
Next
an inner class of MortgageCalculator
, so that it can automatically see the member variables. Next
设为MortgageCalculator
的内部类,以便它可以自动查看成员变量。 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.