2011-08-26 70 views

回答

1

您正在查找的代碼是SWT瀏覽器小部件片段之一。見render HTML from memory

import org.eclipse.swt.*; 
import org.eclipse.swt.browser.*; 
import org.eclipse.swt.layout.*; 
import org.eclipse.swt.widgets.*; 

public class Snippet136 { 
    public static void main(String [] args) { 
     String html = "<HTML><HEAD><TITLE>HTML Test</TITLE></HEAD><BODY>"; 
     for (int i = 0; i < 100; i++) html += "<P>This is line "+i+"</P>"; 
     html += "</BODY></HTML>"; 

     Display display = new Display(); 
     Shell shell = new Shell(display); 
     shell.setLayout(new FillLayout()); 
     Browser browser; 
     try { 
      browser = new Browser(shell, SWT.NONE); 
     } catch (SWTError e) { 
      System.out.println("Could not instantiate Browser: " + e.getMessage()); 
      display.dispose(); 
      return; 
     } 
     browser.setText(html); 
     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) 
       display.sleep(); 
     } 
     display.dispose(); 
    } 
} 

還有其他幾個片段可能成爲得心應手你..檢查SWT Browser's widget snippets

1

在您的內存中添加一行HTML將執行此操作。

例如,假設您的文件page1.html等定位在本地計算機在C:/myHTML,那麼你需要在這個如下內存HTML字符串添加到您的,

String html="<HTML><HEAD><TITLE>HTML Test</TITLE>" 
      +"<BASE href=\"file:///C:myHTML\\/\" >" 
      +"<HEAD>" 
      +"<BODY><A src=\"page1.html\">Page1</A></BODY></HTML>" 

這爲我工作在Win 7。