gpt4 book ai didi

java - 如何将 Double 值从一个 ActionListener 方法引入到另一个方法中以执行计算? java

转载 作者:行者123 更新时间:2023-11-30 02:43:02 25 4
gpt4 key购买 nike

我正在创建一个基本二次公式计算器的 JFrame 程序。我创建了一个 Action 监听器,将三个文本字段转换为三个 Double 值,等于 a、b 和 c,这些值在二次公式中使用。当用户完成时,我不知道如何将这些放入按钮的其他 ActionListener 中。这些按钮的用途是在用户完成后执行二次方程的计算并在新的 JFrame 上显示答案。这是我的代码,有人对如何执行此操作有建议吗?

编辑:尝试执行计算时出现错误,告诉我“找不到符号”

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.math.*;


public class GUISource extends JFrame {

private JTextField textA;
private JTextField textB;
private JTextField textC;
private JLabel labA;
private JLabel labB;
private JLabel labC;
private JButton butA;
private JButton butB;
private JButton butC;

public GUISource(){


super ("Quadratic Equatic");
setLayout (new FlowLayout());

labA = new JLabel("Enter your A value:");

add(labA);

textA = new JTextField(30);

add(textA);

butA = new JButton("Enter");

//add(butA);

labB = new JLabel("Enter your B value:");

add(labB);

textB = new JTextField(30);

add(textB);

//butB = new JButton("Enter");

//add(butB);

labC = new JLabel("Enter your C value:");

add(labC);

textC = new JTextField(30);

add(textC);

//butC = new JButton("Enter");

//add(butC);

add(butA);



listenPlease handle = new listenPlease();
//labA.addActionListener(handle);
//labB.addActionListener(handle);
//labC.addActionListener(handle);
//butA.addActionListener(handle);
//butB.addActionListener(handle);
//butC.addActionListener(handle);
textA.addActionListener(handle);
textB.addActionListener(handle);
textC.addActionListener(handle);

buttonListen handler = new buttonListen();

butA.addActionListener(handler);



}


private class listenPlease implements ActionListener{

public void actionPerformed (ActionEvent e){

String transfer = "";

if (e.getSource() == textA){
transfer = String.format("field 1 : %s", e.getActionCommand());
double a = Integer.parseInt(transfer);

}
else if(e.getSource() == textB){
transfer = String.format("field 2: %s", e.getActionCommand());
double b = Integer.parseInt(transfer);

}
else if(e.getSource() == textC){
transfer = String.format("frield 3: %s", e.getActionCommand());
double c = Integer.parseInt(transfer);

}

}


}

private class buttonListen implements ActionListener{

//private double a;
//private double b;
//private double c;


public void actionPerformed (ActionEvent ButtonPress){


JFrame Answer = new JFrame("Answer");

Answer.setSize(200, 200);

double d = (Math.pow(b, 2)+(4 * a * c));



}


}


}

最佳答案

您可以将三个 double 放入静态变量中:

public class GUISource extends JFrame {
//same as before
private JTextField textA;
private JTextField textB;
private JTextField textC;
private JLabel labA;
private JLabel labB;
private JLabel labC;
private JButton butA;
private JButton butB;
private JButton butC;

public static double a =0,b=0,c=0;

然后在你的actionListener中:

if (e.getSource() == textA){
transfer = String.format("field 1 : %s", e.getActionCommand());
double a = Integer.parseInt(transfer);
GUISource.a = a;
}
//similarly for b and c

关于java - 如何将 Double 值从一个 ActionListener 方法引入到另一个方法中以执行计算? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41078556/

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