2016-08-02 88 views
0

好吧,我是Java的noob,這只是讓我。Java,在類之間傳遞值

我有一個按鈕,調用一些後臺代碼運行的類來檢查磁帶驅動器是聯機,脫機還是繁忙。

按鈕代碼:

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而拒絕用戶的許可才能繼續


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"); 
    }  

這裏是我的btnRunBackup類

public class btnRunBackup {  
private String s; 

public void checkStatus() { 

    String s, path = null; 
    Process p; 


    try{//try1 

     //create a temp file named tempfilexxx.tmp 
     File temp = File.createTempFile("tempfile", ".tmp"); 
     //get file path 
     path = temp.getAbsolutePath(); 
     System.out.println("checkStatus: " + path); 


    //write to tempfilexxx.tmp 
     BufferedWriter bw = new BufferedWriter(new FileWriter(temp)); 
     try{// try2 
      //set p = to the content of ls home 
      p = Runtime.getRuntime().exec("ls /home | grep ariel"); 
      BufferedReader br = new BufferedReader(
       new InputStreamReader(p.getInputStream())); 
      //write content of p to tempfilexxx.tmp line by line 
      while ((s = br.readLine()) != null) 
       bw.write(s + "\n"); 
      //close BufferedReader 
      br.close(); 
     }catch (Exception e){} //END OF try2 

     //close BufferedWriter 
     bw.close(); 

     /* 
     Now that we ran the 'mt -f /dev/nst0 status command under home we 
     will filter for one of the following strings 
     (for testing we will use ls -la /home and filter for ariel) 

     We will do this by calling the checkStatus method 
     */ 

     this.statusControl(path); 

    }catch(IOException e){   
     e.printStackTrace(); 

    }// END OF try1 

}// END OF listDir 

//throws FileNotFoundException for bufferedReader if file not found 
public void statusControl(String param) throws FileNotFoundException, IOException{ 
    /* 
    On production code there will be 4 possible conditions: 
     1. ONLINE - ready to write (currently we will use ariel) 
     2. DR_OPEN - no tape available 
     3. /dev/nst0: Device or resource busy - resource bussy 
     4. If other than stated above give error 1000 
    */ 
    System.out.println("statusControl: " + param); 

    String ONLINE = "arielvz", 
      OPEN = "DR_OPEN", 
      BUSSY = "Device or resource busy", 
      sCurrentLine; 

    //Scan file line by line for one of the above options 
    BufferedReader br = new BufferedReader(new FileReader(param)); 
    while ((sCurrentLine = br.readLine()) != null){ 
     //Tape is online and ready for writing 
     if (sCurrentLine.contains(ONLINE)){ 
      System.out.println("found ariel"); 
     } 
     //There is no tape in the tape drive 
     else if (sCurrentLine.contains(OPEN)){ 
      //lblRunBck should tell the user to put a tape in the drive 
      System.out.println("No tap in tape drive"); 
     } 
     else if (sCurrentLine.contains(BUSSY)){ 
      //lblRunBck should notify user that the resource is in use 
      System.out.println("Device or resource bussy"); 
     } 
     else{ 
      //Something unexpected happend 
      System.out.println("Error 1001: Please notify Administrator"); 
     } 

    } 

}//END OF statusControl 

public String returnHandler(String param){ 
    return param; 
    } 
} 

莫比這更清楚

+0

我想你想實施「回調」。看看http://stackoverflow.com/questions/18279302/how-do-i-perform-a-java-callback-between-classes –

+0

嗨,感謝但不是它,我沒有提到這個在本地運行服務器。這將有助於稍後的一些東西。 –

+0

然後..問題是從statusControl返回一個字符串到checkStatus?我沒有看到當前的問題:\ –

回答

1

如果你想的checkStatus到返回一個狀態,然後不要讓它不返回任何東西(一個void函數)

public class btnRunBackup {  
    private String s; 

    public void checkStatus() { 

但要返回錯誤像一個字符串:

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); 
} 
+0

好吧我現在明白了,感謝幫助的人 –