Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Jtextfield的值用作java代码_Java_Textfield_Jtextfield - Fatal编程技术网

将Jtextfield的值用作java代码

将Jtextfield的值用作java代码,java,textfield,jtextfield,Java,Textfield,Jtextfield,我正在开发一个小应用程序,我想得到一个数学函数,以及x(a,b)的范围,并显示它的图形 在某些情况下,我调用了一个方法来执行x的函数。 我从TextField获取函数(ex.f(x)=2*x+1)并将其用作Java代码 比如说: class Myclass extends JFrame{ blah blah ... JLabel lblFx =new JLebel("f(x=)"); JTextfield Fx = new JTextField(); //and

我正在开发一个小应用程序,我想得到一个数学函数,以及x(a,b)的范围,并显示它的图形

在某些情况下,我调用了一个方法来执行x的函数。 我从TextField获取函数(ex.f(x)=2*x+1)并将其用作Java代码

比如说:

class Myclass extends JFrame{
    blah blah ...
    JLabel lblFx =new JLebel("f(x=)");
    JTextfield Fx = new JTextField();

    //and lets say that this method calculate the f(x).
    //get as argument the x
    double calculateFx(double x){
        return  2*x+1; // !!!!BUT HERE I WANT TO GET THIS 2*x+1 FROM TextField!!!!!
    }

}

有什么想法吗?

在某些语言中,您正在寻找的东西被称为“”;然而,Java并没有直接的这样的东西

Java确实有一个支持对用其他语言(如JavaScript、Ruby、Python等)编写的代码进行“eval”的工具,还有一个提供对Java编译器(以及其他工具)的访问的工具。对于高级程序员,另一种方法是使用解析器生成器解析文本字段中的字符串,并以某种方式对其进行解释


不幸的是,对于初学者来说,这些都是高级概念。考虑用另一种支持“EVAL”的语言编写程序。

< P>可以使用Script引擎。请参见下面的一个示例,您可以调整该示例以使用JTextField的内容

注意:
“2x+1”
不是有效的表达式,您需要包含所有运算符,因此在本例中:
“2*x+1”

我发现这个很棒的图书馆

这个包解析和计算浮点数上的数学表达式,比如2+2或cos(x/(2*pi))*cos(y/(2*pi))

设计重点是易用性和有用的错误信息,易于集成到小程序中,以及良好的性能:快速评估和短下载时间。功能和灵活性不是重点,但代码足够简单,不难根据您的喜好进行更改。”

以我为例

class Myclass extends JFrame{
    blah blah ...
    JLabel lblFx =new JLebel("f(x=)");
    JTextfield Fx = new JTextField();
    String formula = Fx.getText();//2*x+1  OR  cos(x) OR lot of other functions

    //and lets say that this method calculate the f(x).
    //get as argument the x
    double calculateFx(double x){
        // The Expr Object that will representing the mathematical expression
        Expr expr = null;

        try {
            // parsing function formula to mathematical expression
            expr = Parser.parse(formula);
        } catch (SyntaxException e) {
            System.err.println(e.explain());
            return;
        }

        // Define(make) what will be the variable in the formula, here x
        Variable vx = Variable.make("x");
        // Set the given value(x) in variable
        vx.setValue(x);

        //return (mathematical) expression's value. See more for Expr lib how work...
        return expr.value();//
    }

}

最后,当项目中的某个地方调用calculateFx(x)时,对于给定的x,您将得到f(x)值

你确定吗?我解释了他的最后一句话“从TextField获取函数(ex.f(x)=2x+1)并将其用作java代码”来暗示我的答案。@Universe如果你想处理更复杂的表达式,你将需要解析字符串并用Javascript等价物替换数学函数。在这种情况下,您需要将
cos
替换为
Math.cos
。一种方法是将其存储在地图中,其中key=cos和value=Math.cos。循环映射并替换字符串中的每个函数。
class Myclass extends JFrame{
    blah blah ...
    JLabel lblFx =new JLebel("f(x=)");
    JTextfield Fx = new JTextField();
    String formula = Fx.getText();//2*x+1  OR  cos(x) OR lot of other functions

    //and lets say that this method calculate the f(x).
    //get as argument the x
    double calculateFx(double x){
        // The Expr Object that will representing the mathematical expression
        Expr expr = null;

        try {
            // parsing function formula to mathematical expression
            expr = Parser.parse(formula);
        } catch (SyntaxException e) {
            System.err.println(e.explain());
            return;
        }

        // Define(make) what will be the variable in the formula, here x
        Variable vx = Variable.make("x");
        // Set the given value(x) in variable
        vx.setValue(x);

        //return (mathematical) expression's value. See more for Expr lib how work...
        return expr.value();//
    }

}