簡體   English   中英

如何在擴展 Swingworker 的類的 done() 方法中調用類?

[英]How to call a class in the done() method of a class which extends Swingworker?

我寫了一個打開 cd/dvd(名為 Cdopener)的類,我有另一個類(名為 Burn),它擴展了 Swingworker,它在它的 done() 方法中做了一些事情,我想創建一個 Cdopener 類的對象它在 Burn 類運行時打開 cd。

這是我的 Cdopener 類:

import java.awt.Desktop;

import java.io.File;
import java.io.PrintWriter;

public class Cdopener {
    public Cdopener() {
        try {
            //********Start VBScript code to open cd tray************
            String a = "Set oWMP = CreateObject(\"WMPlayer.OCX\")" + "\n" 
                + "Set colCDROMs = oWMP.cdromCollection" + "\n" 
                + "For d = 0 to colCDROMs.Count - 1" + "\n" 
                + "colCDROMs.Item(d).Eject" + "\n" 
                + "Next" + "\n" 
                + "set owmp = nothing" + "\n" 
                + "set colCDROMs = nothing" + "\n" 
                + "wscript.Quit(0)";
            //********End VBScript code to open cd tray************

            //Create a vbscript file called OpenCdTray.vbs
            File myCdTrayOpener = new File("OpenCdTray.vbs");

            //Create a PrintWriter object that will use to write into created file
            PrintWriter pw = new PrintWriter(myCdTrayOpener);

            //Write all string in (a) into created file
            System.out.println(a);
            pw.print(a);

            //Flush all resource in PrintWriter to make sure
            //there are no data left in this stream.
            pw.flush();

            //Close PrintWriter and releases any
            //system resources associated with it
            pw.close();

            //Create a Desktop object to open created vbs file(OpenCdTray.vbs).
            //It will open using default application that will use
            //to handle this file in targeted computer.
            //True application to run this file is wscript.exe
            Desktop.getDesktop().open(myCdTrayOpener);

            //Delete created vbs file before terminate application
            myCdTrayOpener.deleteOnExit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

這是 Burn 類的 done() 方法:

protected void done() {

    taskProgressBar.setIndeterminate(false);

    if (this.error.length() == 0) {
        taskProgressBar.setValue(100);
        taskProgressBar.setString("Done");
    }

    // this.resultLbl.getText() +"\n"+getUserName();
    addLog(resultLbl.getText());
    // super.done();
}

我想調用 Cdopener 以便它打開 cd,為此我只是將這部分添加到我的代碼中,以便在 Burn 類的 done() 部分創建一個 Cdopener 對象:

Cdopener cdopener = new Cdopener();

但似乎有些事情應該改變,因為它不會以這種方式打開 CD! 我應該如何在我的 done 方法中調用 Cdopener 類?

嘗試這個

Desktop.getDesktop().open(new File(myCdTrayOpener.getAbsolutePath())); 用於打開您的 vbs 文件以確保它指向正確的文件目錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM