还是SWT爽呀,word,IE想用什么都行

这篇博客对比了SWT和Swing在GUI开发中的优缺点,强调了SWT如何方便地集成Word和IE。作者通过示例代码展示了如何在SWT中创建一个可以打开和保存Word文档的应用,并提到了寻找OLEView的相关信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,不知道有人知道吗?

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值