還是SWT爽呀,word,IE想用什麼都行

來源:互聯網
上載者:User

swing的先天缺陷,jeditpane功能具弱,想用word還得用jacob,而且只能控制沒有編輯控制項.
swt就好多了, word,IE想用什麼直接調就行了,看來以後要多留意swt了.

package gui.test;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OleClientSite;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class WordView extends org.eclipse.swt.widgets.Composite {
 private OleFrame oleFrame1;
 private OleClientSite site;

 /**
  * Auto-generated main method to display this
  * org.eclipse.swt.widgets.Composite inside a new Shell.
  */
 public static void main(String[] args) {
  showGUI();
 }
 /**
  * Auto-generated method to display this
  * org.eclipse.swt.widgets.Composite inside a new Shell.
  */
 public static void showGUI() {
  Display display = Display.getDefault();
  Shell shell = new Shell(display);
  shell.setText("Word in SWT");
  new WordView(shell, SWT.NULL);

  shell.setLayout(new FillLayout());
  shell.layout();
  shell.setSize(600, 400);

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

 public WordView(org.eclipse.swt.widgets.Composite parent, int style) {
  super(parent, style);
  initGUI();
 }

 private void initGUI() {
  try {
   FillLayout thisLayout = new FillLayout(
     org.eclipse.swt.SWT.HORIZONTAL);
   this.setLayout(thisLayout);
   this.setSize(229, 54);
   {
    oleFrame1 = new OleFrame(this, SWT.NONE);
    {
     try {
      site = new org.eclipse.swt.ole.win32.OleClientSite (oleFrame1, org.eclipse.swt.SWT.NONE, "Word.Document");
      
      site.setBounds(0, 0, 104, 54);
      site.doVerb (org.eclipse.swt.ole.win32.OLE.OLEIVERB_SHOW);
     } catch (org.eclipse.swt.SWTException e) {
      String str = "Create OleClientSite Error" + e.toString ();
      System.out.println (str);
      return;
     }
    }
   }

   this.layout();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 private void open(File file) {
  site = null;
  try {
   site = new org.eclipse.swt.ole.win32.OleClientSite (oleFrame1, org.eclipse.swt.SWT.NONE, file);
   
   site.setBounds(0, 0, 104, 54);
   site.doVerb (org.eclipse.swt.ole.win32.OLE.OLEIVERB_SHOW);
  } catch (org.eclipse.swt.SWTException e) {
   String str = "Create OleClientSite Error" + e.toString ();
   System.out.println (str);
   return;
  }
 }
 
 public void open(String strName) {
  //site.(new File(strName),true);
  open(new File(strName));
 }
 
 public void save(String strName) {
  site.save(new File(strName),true);
 }

}

package gui.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;

public class SwtDemo {

 /**
  * Launch the application
  * @param args
  */
 public static void main(String[] args) {
  final Display display = Display.getDefault();
  final Shell shell = new Shell();
  shell.setSize(500, 375);
  shell.setText("SWT Application");
  //

  final WordView composite = new WordView(shell, SWT.NULL);
  composite.setBounds(0, 0, 490, 295);

  final Button open = new Button(shell, SWT.NONE);
  open.setText("開啟文檔");
  open.setBounds(315, 310, 70, 20);

  final Button save = new Button(shell, SWT.NONE);
  save.setBounds(412, 310,70, 20);
  save.setText("儲存文檔");
  shell.layout();
  
  open.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent event) {
    FileDialog dlg = new FileDialog(shell, SWT.OPEN);
    dlg.setFilterNames(new String[]{"word文檔(*.doc)"});
    dlg.setFilterExtensions(new String[]{"*.doc"});

    String fileName = dlg.open();
    if (fileName != null) {
     composite.open(fileName);
    }
   }
  });

  save.addSelectionListener(new SelectionAdapter() {
   public void widgetSelected(SelectionEvent event) {
    FileDialog dlg = new FileDialog(shell, SWT.SAVE);
    dlg.setFilterNames(new String[]{"word文檔(*.doc)"});
    dlg.setFilterExtensions(new String[]{"*.doc"});

    String fileName = dlg.open();
    if (fileName != null) {
     composite.save(fileName);
    }
   }
  });
  
  shell.open();
  while (!shell.isDisposed()) {
   if (!display.readAndDispatch())
    display.sleep();
  }
 }

}

 

閑話少說,把我做的測試程式貼上(初學swt,用Designer做的介面,核心代碼都是網上抄來的).
說實話,我覺得swt的實現方式真的沒swing舒服,高手不要笑我.

另那裡有下vs2003的oleview,不知道有人知道嗎?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.