[英]How to incorporate 'public int' in actionPerformed button?
这是一些ode,在bytes.getText()中声明值后,它通过递归函数计算公式。 但是java并不认为在actionPerformed按钮下有任何'public'。 我从来没有使用过其中一种......但你认为ActionListener可以做到这一点吗? 有任何想法吗?
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;
}
}
编辑:
我想我可能已经把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
您不能将方法放在Java中的另一个方法中。 将这3个方法移动到calculateButtonActionPerformed()
之外,与其相同。 然后,您可以在calculateButtonActionPerformed()
调用它们。
例如,您不能在Java中执行此操作:
public void method1() {
public void method2() {
}
}
你必须这样做:
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();
}
这就是Joe F在他的回答中解释的。 试试这种方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.