如何在类的done()方法中调用类来扩展Swingworker?

时间:2015-05-11 06:03:39

标签: java object call swingworker cd

我写了一个打开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!我该如何在完成方法中调用类Cdopener?

1 个答案:

答案 0 :(得分:0)

试试这个

Desktop.getDesktop()。open(new File(myCdTrayOpener.getAbsolutePath())); 用于打开vbs文件以确保它指向正确的文件目录。