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