Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java asyncExec方法未调用Runnable类的run方法_Java_Swing_Swt - Fatal编程技术网

Java asyncExec方法未调用Runnable类的run方法

Java asyncExec方法未调用Runnable类的run方法,java,swing,swt,Java,Swing,Swt,我想在java的Jframe中嵌入MicrosoftWord。这就是为什么我有以下代码: public class AbrirWordJFrame { static OleClientSite clientSite; static OleFrame frame; public static void main(String[] args) { final Display display = new Display(); final She

我想在java的Jframe中嵌入MicrosoftWord。这就是为什么我有以下代码:

public class AbrirWordJFrame {
    static OleClientSite clientSite;
    static OleFrame frame;

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);

        JFrame jframe=new JFrame("Mi jframe");
        final Canvas canvas=new Canvas();
        jframe.getContentPane().add(canvas);
        jframe.setSize(800, 600);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);

        System.out.println("Before Async"); 

        display.asyncExec(new Runnable() {
            public void run() {
                System.out.println("In Async method");
                Shell shell = SWT_AWT.new_Shell(display, canvas);
                shell.setSize(800, 600);

                //abrimos un word
                shell.setText("Word Example");
                shell.setLayout(new FillLayout());
                try {
                    frame = new OleFrame(shell, SWT.NONE);
                    //esto abre un documento existente
                    clientSite = new OleClientSite(frame, SWT.NULL, new File("prueba.doc"));
                    //esto abre un documento en blanco
//                  clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
                    addFileMenu(frame);
                } catch (SWTError e) {
                    System.out.println("Unable to open activeX control");
                    display.dispose();
                    return;
                }
                //fin abrimos un word
                //abrimos un navegador
//              Browser browser = new Browser(shell, SWT.NONE);
//              browser.setLayoutData(new GridData(GridData.FILL_BOTH));
//              browser.setSize(800, 600);
//              browser.setUrl("http://www.google.com");
                //fin abrimos un navegador
                shell.open();

            }
        }); 

       // display d= new Display(new runnable);


        //el titulo
//      shell.setText("Word Example");
//      shell.setLayout(new FillLayout());
//      try {
//          frame = new OleFrame(shell, SWT.NONE);
//          //esto abre un documento existente
//          clientSite = new OleClientSite(frame, SWT.NULL, new File("prueba.doc"));
//          //esto abre un documento en blanco
////            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
//          addFileMenu(frame);
//      } catch (SWTError e) {
//          System.out.println("Unable to open activeX control");
//          display.dispose();
//          return;
//      }
//      shell.setSize(800, 600);
//      shell.open();
//      
        while (!shell.isDisposed()) {
            /*if (!display.readAndDispatch())
                display.sleep();*/
        }
        display.dispose();
    }

    static void addFileMenu(OleFrame frame) {
        final Shell shell = frame.getShell();
        Menu menuBar = shell.getMenuBar();
        if (menuBar == null) {
            menuBar = new Menu(shell, SWT.BAR);
            shell.setMenuBar(menuBar);
        }
        MenuItem fileMenu = new MenuItem(menuBar, SWT.CASCADE);
        fileMenu.setText("&File");
        Menu menuFile = new Menu(fileMenu);
        fileMenu.setMenu(menuFile);
        frame.setFileMenus(new MenuItem[] { fileMenu });

        MenuItem menuFileOpen = new MenuItem(menuFile, SWT.CASCADE);
        menuFileOpen.setText("Open...");
        menuFileOpen.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                fileOpen();
            }
        });
        MenuItem menuFileExit = new MenuItem(menuFile, SWT.CASCADE);
        menuFileExit.setText("Exit");
        menuFileExit.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });
    }

    static void fileOpen() {
        FileDialog dialog = new FileDialog(clientSite.getShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.doc" });
        String fileName = dialog.open();
        if (fileName != null) {
            clientSite.dispose();
            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document", new File(fileName));
            clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        }
    }
}
我得到以下例外情况:

Exception in thread "main" org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.IllegalArgumentException: Argument not valid)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Unknown Source)
    at org.eclipse.swt.widgets.Display.runAsyncMessages(Unknown Source)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
    at AbrirWordJFrame.main(AbrirWordJFrame.java:95)
Caused by: java.lang.IllegalArgumentException: Argument not valid
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.SWT.error(Unknown Source)
    at org.eclipse.swt.ole.win32.OLE.error(Unknown Source)
    at org.eclipse.swt.ole.win32.OLE.error(Unknown Source)
    at org.eclipse.swt.ole.win32.OleClientSite.<init>(Unknown Source)
    at AbrirWordJFrame$1.run(AbrirWordJFrame.java:55)
    at org.eclipse.swt.widgets.RunnableLock.run(Unknown Source)
    ... 4 more
线程“main”org.eclipse.swt.swt异常:无法执行runnable(java.lang.IllegalArgumentException:参数无效) 位于org.eclipse.swt.swt.error(未知源) 位于org.eclipse.swt.swt.error(未知源) 位于org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(未知源) 位于org.eclipse.swt.widgets.Display.runAsyncMessages(未知源) 位于org.eclipse.swt.widgets.Display.readAndDispatch(未知源) 在AbrirWordJFrame.main(AbrirWordJFrame.java:95) 原因:java.lang.IllegalArgumentException:参数无效 位于org.eclipse.swt.swt.error(未知源) 位于org.eclipse.swt.swt.error(未知源) 位于org.eclipse.swt.swt.error(未知源) 位于org.eclipse.swt.ole.win32.ole.error(未知源) 位于org.eclipse.swt.ole.win32.ole.error(未知源) 位于org.eclipse.swt.ole.win32.OleClientSite。(未知来源) 在AbrirWordJFrame$1.run(AbrirWordJFrame.java:55) 位于org.eclipse.swt.widgets.RunnableLock.run(未知源) ... 4更多
但asyncExec方法未调用runnable类的run方法。我在网上搜索了很多,但什么也没找到。请帮帮我

如果您没有运行SWT事件分派循环,则必须运行此循环才能使
asyncExec
(以及SWT的许多其他部分)正常工作

你有:

while (!shell.isDisposed()) {
    /*if (!display.readAndDispatch())
        display.sleep();*/
}
那些注释掉的行是事件分派循环。你必须致电:

while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}

但是您正在混合Swing和SWT-您不应该这样做,因为很难正常工作。使用Swing或SWT,但不能同时使用两者。您可以使用
SWT\u AWT
类从SWT调用Swing代码,但这应该是最后的选择。

问题稍微经过编辑。请查看更新后的问题。如果我删除了注释,则我遇到了问题中所述的异常。您的OleClientSite构造函数有问题,对此我一无所知。我尝试回答以下问题:我不知道,因为这是一个仅限Windows的API,我不运行Windows。您正在从构造函数中的某个位置获取
IllegalArgumentException
,因此出现了问题-可能
File
参数必须引用实际存在的文件或类似文件。