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

标签 java swing actionlistener

我正在创建一个基本二次公式计算器的 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/

相关文章:

java - Java 中的设置方法

java - 在其他 JPanel 内重新绘制 JPanel

java - 调用 dispose() 不会关闭 JFrame

JPanel ActionsListener 中的 Java 按钮不起作用

java - 如何让 ActionListener 根据 actionPerformed 执行不同的事件?

java - 基本 Java 数组列表

java - java中的输入参数

java - KeyListener/KeyBinding 不一致触发

java - 使用 ActionListener 更新时钟 - Java

java - 使用命令行相当于 dcm4che3 的storescp 中的 dcmrcv -native