Java,在类之间传递值

时间:2016-08-02 14:22:28

标签: java swing awt

好的,所以我是Java的菜鸟,这就是我。

我有一个按钮,用于调用运行某些后台代码的类,以检查磁带机是否处于联机,脱机或忙碌状态。

按钮代码:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
    return true;
}


@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
        float velocityX, float velocityY) {
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
    return true;
}

然后我有单独的private void btnRunBckActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: btnRunBackup runBackupObject = new btnRunBackup(); runBackupObject.checkStatus(); lblRunBck.setText("Errors go here"); } 文件class

btnRunBackup

我想将public class btnRunBackup{ public void checkStatus(){ /* Here I simply create a tempfile and run some linux commands via getRuntime and print the output to the tempfile Then I call my second method passing the absolute file path of the tempfile */ this.statusControl(path); }catch(IOException e){ e.printStackTrace(); public void statusControl(String param) throws FileNotFoundException, IOException{ /* Here I use BufferedReader to go through the tempfile and look for as series of 3 different strings. I use a if else if statement for flow control depending on what string was found. string 1 will call a new Jframe if string 2, 3 or none of them are found the is where I am stuck at } } 值返回String。 原因是btnRunBckActionPerformed()最初将根本不显示任何文本,但是例如用户点击按钮并且资源正好忙,那么我想运行lblRunBck;在lblRunBck.setText(param)上拒绝用户继续的权限

lblRunBck

这是我的btnRunBackup类

private void btnRunBckActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        String text;

        btnRunBackup runBackupObject = new btnRunBackup();

        runBackupObject.checkStatus();

        lblRunBck.setText("Errors go here");
    }    

Maby这将使其更加清晰

1 个答案:

答案 0 :(得分:1)

如果你想让checkStatus返回状态,那么不要让它返回任何内容(void函数)

public class btnRunBackup {    
    private String s;

    public void checkStatus() {

但是让它像String一样返回错误:

public class btnRunBackup {    
    private String s;

    public String checkStatus() {
        String error = null; // by default no error
          ... do whatever you need to find out the error
              .... 
              error = "error is: xxx ";
        return error; // return null (no error ) or what you found
    }

在调用代码时更改逻辑,以显示checkStatus返回的错误

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

        // TODO add your handling code here:
        String error;

        btnRunBackup runBackupObject = new btnRunBackup();

        error = runBackupObject.checkStatus();

        lblRunBck.setText(error == null ? "No error" : error);
}