English 中文(简体)
按下按钮时显示返回的值吗?
原标题:Displaying a returned value when a button is pressed?
  • 时间:2012-05-28 02:43:55
  •  标签:
  • java
  • awt

我这里有一些代码 计算一个阵列的最大值 :

 public static int getMaxValue(int[] marks){
     int maxValue = marks[0];
     for(int i=1;i < marks.length;i++){
         if(marks[i] > maxValue){
             maxValue = marks[i];
         }
     }
     return maxValue;
 }

我想显示用户按下按钮时的最大值。 这是我目前拥有的, 但是它不起作用 :

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

    maxValue mv = new maxValue ();
    analyzeTextArea.setText("Maximum:" + maxValue.toString());

}

谢谢你的帮助!

最佳回答

我想你在寻找 类似这样的东西...

private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    analyzeTextArea.setText("Maximum:" + getMaxValue(arrayOfValues));
}

要调用一种方法,除了调用 methodName (inputValues) - 您不需要做任何其他工作, 但无法通过使用 new 方法来创建该方法的实例Name ()

如果该方法位于不同的类中, 且与您的情况类似, 是 stistic 方法, 您可以做到这一点...

private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    analyzeTextArea.setText("Maximum:" + MyClass.getMaxValue(arrayOfValues));
}

否则,如果该方法位于另一类,且不是 stistic ,那么您先创建该类实例,然后调用该方法...

private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    MyClass example = new MyClass();
    analyzeTextArea.setText("Maximum:" + example.getMaxValue(arrayOfValues));
}
问题回答

由于您的方法是静态的, 您可以简单地使用包含该方法的类的名称来称呼它 。

analyzeTextArea.setText("Maximum:" + YourClassNameHere.getMaxValue());

您无法即时转换方法 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签