简体   繁体   English

如何将值从JPanel传递到NetBeans中的JFrame

[英]How to pass a value from a JPanel to a JFrame in netbeans

I have the following method in my JPanel: 我的JPanel中有以下方法:

public void detectionTime(){
      detectStartTime = System.currentTimeMillis();
      long detectDuration = System.currentTimeMillis() - detectStartTime;
      performance.lblDetectTime.setText(detectDuration + "ms");
  }

I want to print or output the detectDuration variable in a label ie lblDetectTime in a JFrame. 我想在JFrame的标签(即lblDetectTime)中打印或输出detectDuration变量。 I have created an instance of the JFrame as "performance" and I have also created an instance of the JPanel into the JFrame in order to access the method. 我已将JFrame的实例创建为“性能”,并且还将JPanel的实例创建到JFrame中以访问该方法。

Why pass, when you can directly set it............. 为什么可以通过,何时可以直接设置.............

If still you want to pass it... then change the return type of detectionTime() method in JPanel. 如果仍然要传递它,则在JPanel中更改detectionTime()方法的返回类型。

public String detectionTime(){

detectStartTime = System.currentTimeMillis();
long detectDuration = System.currentTimeMillis() - detectStartTime;
return detectDuration + "ms";

 }

Edited Again: 再次编辑:

Do this from class that extends the JFrame (ie your container) 从扩展JFrame的类(即您的容器)中执行此操作

IblDetectTime.setText(MyPanel.detectionTime());

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

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