简体   繁体   English

如何在actionPerformed按钮中加入'public int'?

[英]How to incorporate 'public int' in actionPerformed button?

This is some ode that after declaring values in the fields.getText() it calculates a formula by a recursion function. 这是一些ode,在bytes.getText()中声明值后,它通过递归函数计算公式。 However java does not see to like having any 'public' under actionPerformed button. 但是java并不认为在actionPerformed按钮下有任何'public'。 I've never used one of these...but you do you think an ActionListener could do the job? 我从来没有使用过其中一种......但你认为ActionListener可以做到这一点吗? Any ideas? 有任何想法吗?

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {

         public int Method() { //underlined red
            int n = Integer.parseInt(objectsChooseField.getText());
            int r = Integer.parseInt(chooseFromField.getText());
            int result = C( n, r );
        }

        public int C( int n, int r ) { //underlined
            int res = faculty( n ) / ( faculty( r ) * ( n - r ));
            return res;
        }

        public int faculty( n ) { //underlined
            if ( n > 1 )
                return n * faculty( n - 1 );
            return 1;
        }

    }

EDIT: 编辑:

I think I'm might have taken Joe's answer a little to literally...but do you mean something like this...? 我想我可能已经把Joe的答案稍微提了一下......但是你的意思是这样吗......?

public int Method() { //moved above actionPerformed

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {

                int n = Integer.parseInt(objectsChooseField.getText()); //continued method?
                int r = Integer.parseInt(chooseFromField.getText());
                int result = C( n, r );
            }
//rest of code below

You can't put methods inside of another method in Java. 您不能将方法放在Java中的另一个方法中。 Move those 3 methods outside of, and at the same level as, calculateButtonActionPerformed() . 将这3个方法移动到calculateButtonActionPerformed()之外,与其相同。 You can then call them from within calculateButtonActionPerformed() . 然后,您可以在calculateButtonActionPerformed()调用它们。

For example, you can't do this in Java: 例如,您不能在Java中执行此操作:

public void method1() {
    public void method2() {
    }
}

You have to do this: 你必须这样做:

public void method1() {
}

public void method2() {
}
public int method() { //your handlers
}
public int c(int n, int r) { //your handlers
}
public int faculty(int n) { //your handlers
}
public void calculateButtonActionPerformed(ActionEvent e) {
    method();
}

This is what Joe F explained in his answer. 这就是Joe F在他的回答中解释的。 Try it this way. 试试这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM