`

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

阅读更多

 

     网上搜索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();
					}
				});
 

 

3
0
分享到:
评论
5 楼 z995204127 2014-08-31  
我去,发多了,太激动
4 楼 z995204127 2014-08-31  
楼主,我去,太尼玛给力了,我搞了一天没搞定,原来要用一个线程包装一下。万分感谢,我去。
3 楼 z995204127 2014-08-31  
楼主,我去,太尼玛给力了,我搞了一天,原来要用一个线程包装下。OK了,十分感谢
2 楼 pywepe 2011-12-25  
不知所云
1 楼 zqfddqr 2010-11-05  
留个记号谢谢博主

相关推荐

Global site tag (gtag.js) - Google Analytics