gpt4 book ai didi

java - 如何从一个类获取另一个 Action 监听器类的变量信息?

转载 作者:行者123 更新时间:2023-12-02 12:57:26 31 4
gpt4 key购买 nike

这是我的两个文件类的代码。为了使变量在 Action 监听器 JFrame 中传输和打印,我必须添加/修复什么?在“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);

}
}

Action 监听器:

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

}
}

最佳答案

主方法中的变量是局部变量,因此您的监听器将无法看到它们。

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

  • Next创建为MortgageCalculator的内部类,以便它可以自动看到成员变量。
  • 使 MortgageCalculator 实现 ActionListener,并将 actionPerformed 方法移至结果类中。然后,您可以编写 nextButton.addActionListener(new Next());,而不是编写 nextButton.addActionListener(this); - 但仅限于非静态方法内(所以不是 main - 您需要让 mainMortgageCalculator 实例上调用非静态方法)。

关于java - 如何从一个类获取另一个 Action 监听器类的变量信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44380660/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com