【原创】swing 按钮点击 调用swt窗口(SWT浏览器)browser

11 篇文章 0 订阅
5 篇文章 0 订阅

 

     网上搜索swing 和swt 都是关于两者的区别及介绍,有时候想混用没有详细介绍。有swt_awt桥,albireo 等方法。

下面我介绍我得解决方法,为了弄这个昨晚到两点才睡,希望看了的给我留言点鼓励哦:

 

package  db.common.tool;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.swtdesigner.SWTResourceManager;

public class MyBroswer {

	
	public MyBroswer() {
		
	}
	public MyBroswer(File file,String title) {
		super();
		
		   
		    Display display = new Display();
		    final Shell shell = new Shell(display);
		    shell.setImage(SWTResourceManager.getImage(MyBroswer.class, "/db/gui/icon/ S.gif"));
		    shell.setText(title+"浏览");
		    shell.setSize(1220, 831);	
		    Dimension dem=Toolkit.getDefaultToolkit().getScreenSize();
		    int sHeight=dem.height;
		    int sWidth=dem.width;
		    int fHeight=shell.getSize().y;
		    int fWidth=shell.getSize().x;
		    shell.setLocation((sWidth-fWidth)/2, (sHeight-fHeight)/2);

		    shell.setLayout(new FormLayout());

		    final Browser browser = new Browser(shell, SWT.BORDER);
		    {
		    	FormData formData = new FormData();
		    	formData.top = new FormAttachment(0, 10);
		    	formData.bottom = new FormAttachment(100, -10);
		    	formData.left = new FormAttachment(0, 10);
		    	formData.right = new FormAttachment(100, -10);
		    	browser.setLayoutData(formData);
		    }

		    shell.open();
		    String path=file.getAbsolutePath();
		    browser.setUrl(path);
		    
		    while (!shell.isDisposed()) {
		      if (!display.readAndDispatch())
		        display.sleep();
		    }
		    display.dispose();
	}
	

public static void main(String[] args) {
	 File file = new File("data\\4\\law\\Index.html"); 
//	 File file = new File("DATA\\3\\zhanx_imglist.asp.htm");
//	File file = new File("DATA\\1\\sc35.htm");
	new MyBroswer(file,"标题");
  }	
}

 以上是SWT 浏览器,可以单独运行没有问题,关键是swing控件调用!一定要用线程调用。

	class MyBrowserFlagThread extends Thread {
		
		public void run() {
			File file = new File("DATA\\1\\sc35.htm");
			new MyBroswer(file,"世界国旗大全");
		}
	}

 swing JButton 调用:

button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						MyBrowserFlagThread ut = new MyBrowserFlagThread( );
						ut.start();
					}
				});
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值