基于eclipse swt做java浏览器内嵌等功能
生活随笔
收集整理的這篇文章主要介紹了
基于eclipse swt做java浏览器内嵌等功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://wangzi6hao.iteye.com/blog/531550
swt-3.5M4-win32-win32-x86.zip 包下載地址:?http://download.csdn.net/source/921856
csdn上有些朋友說我這個是騙人的包。我無語。幾個dome,你們自己去看吧。別不會用就說我人品有問題。
import java.util.ArrayList;import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.OpenWindowListener; import org.eclipse.swt.browser.VisibilityWindowListener; import org.eclipse.swt.browser.WindowEvent; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell;public class BrowserTest1 { static final ArrayList urls = new ArrayList(); // @jve:decl-index=0: static boolean canRemove = false; public static void main(String[] args) {Display display = new Display();Shell shell = new Shell(display);shell.setText("jog36行業招聘");Image image = new Image(shell.getDisplay(), "Favicon.ico");shell.setImage(image);shell.setLayout(new FillLayout());Browser browser = new Browser(shell, SWT.NONE);initialize(display, browser);shell.open();browser.setUrl("www.job36.com");while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}display.dispose(); }static void initialize(final Display display, Browser browser) {browser.addListener(SWT.MouseDown, new Listener() {public void handleEvent(Event event) {System.out.println("event.time:" + event.time);}});browser.addOpenWindowListener(new OpenWindowListener() {public void open(WindowEvent event) {// Embed the new windowfinal Shell shell = new Shell(display);shell.setText("New Window");shell.setLayout(new FillLayout());final Browser browser = new Browser(shell, SWT.NONE);initialize(display, browser);event.browser = browser;event.display.asyncExec(new Runnable() {public void run() {String url = browser.getUrl();System.out.println(url);System.out.println(browser.getText());if (urls.contains(url)){//flag to chek if the window is closed automaticcanRemove = false;shell.close();}else{canRemove = true;urls.add(url);}}});}});browser.addVisibilityWindowListener(new VisibilityWindowListener() {public void hide(WindowEvent event) {Browser browser = (Browser) event.widget;Shell shell = browser.getShell();shell.setVisible(false);}public void show(WindowEvent event) {Browser browser = (Browser) event.widget;Shell shell = browser.getShell();if (event.location != null)shell.setLocation(event.location);if (event.size != null) {Point size = event.size;shell.setSize(shell.computeSize(size.x, size.y));}if (event.addressBar || event.menuBar || event.statusBar|| event.toolBar) {// Create widgets for the address bar, menu bar, status bar and/or tool bar// leave enough space in the Shell to accomodate a Browser of the size// given by event.size}shell.open();}});browser.addDisposeListener(new DisposeListener(){public void widgetDisposed(DisposeEvent event){Browser browser = (Browser) event.widget;if (canRemove)urls.remove(browser.getUrl());Shell shell = browser.getShell();// shell.close();}}); }}
import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.CloseWindowListener; import org.eclipse.swt.browser.LocationEvent; import org.eclipse.swt.browser.LocationListener; import org.eclipse.swt.browser.OpenWindowListener; import org.eclipse.swt.browser.ProgressEvent; import org.eclipse.swt.browser.ProgressListener; import org.eclipse.swt.browser.StatusTextEvent; import org.eclipse.swt.browser.StatusTextListener; import org.eclipse.swt.browser.WindowEvent; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell;/** * This class implements a web browser */ public class TestBrowser {// The "at rest" text of the throbberprivate Label throbber;private Button button;private Combo url;private Button button_stop;private Button button_refresh;private Browser browser;private Browser browser2;private Label status;private Button button_forward;private Button button_back;private static final String AT_REST = "Ready";private String[] urlList = new String[256];int urlListItemCount = 0;/*** Runs the application** @param location* the initial location to display*/public void run(String location) {Display display = new Display();Shell shell = new Shell(display);shell.setText("Job36 行業招聘網 CRM系統");createContents(shell, location);shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch()) {display.sleep();}}display.dispose();}/*** Creates the main window's contents** @param shell* the main window* @param location* the initial location*/public void createContents(Shell shell, String location) {shell.setLayout(new FormLayout());// Create the composite to hold the buttons and text fieldComposite controls = new Composite(shell, SWT.NONE);FormData data = new FormData();data.top = new FormAttachment(0, 0);data.left = new FormAttachment(0, 0);data.right = new FormAttachment(100, 0);controls.setLayoutData(data);// Create the status barstatus = new Label(shell, SWT.NONE);data = new FormData();data.left = new FormAttachment(0, 0);data.right = new FormAttachment(100, 0);data.bottom = new FormAttachment(100, 0);status.setLayoutData(data);// Create the web browserbrowser = new Browser(shell, SWT.BORDER);browser.addOpenWindowListener(new OpenWindowListener() {public void open(WindowEvent event) {Browser browsertest = (Browser)event.widget;final Shell shell = browser.getShell();System.out.println(null==browsertest?"為null":browsertest.getUrl()); // String urlStr = event.browser.getUrl(); // int flag = 1; // browser.setVisible(false); // browser2.setVisible(true); // event.browser = browser2; // for (int i = 0; i < urlListItemCount; i++) { // if (urlList[i].equals(urlStr)) { // flag = 0; // } // } // if (flag == 1) { // urlList[urlListItemCount] = urlStr; // url.add(urlStr); // url.setText(urlStr); // urlListItemCount++; // }}});data = new FormData();data.top = new FormAttachment(controls);data.bottom = new FormAttachment(status);data.left = new FormAttachment(0, 0);data.right = new FormAttachment(100, 0);browser.setLayoutData(data);// Create the web browserbrowser2 = new Browser(shell, SWT.BORDER);browser2.addOpenWindowListener(new OpenWindowListener() {public void open(WindowEvent arg0) {String urlStr = arg0.browser.getUrl();int flag = 1;browser2.setVisible(false);browser.setVisible(true);arg0.browser = browser;for (int i = 0; i < urlListItemCount; i++) {if (urlList[i].equals(urlStr)) {flag = 0;}}if (flag == 1) {urlList[urlListItemCount] = urlStr;url.add(urlStr);url.setText(urlStr);urlListItemCount++;}}});data = new FormData();data.top = new FormAttachment(controls);data.bottom = new FormAttachment(status);data.left = new FormAttachment(0, 0);data.right = new FormAttachment(100, 0);browser2.setLayoutData(data);// Create the controls and wire them to the browsercontrols.setLayout(new GridLayout(7, false));// Create the back buttonbutton_back = new Button(controls, SWT.PUSH);button_back.setText("后退");button_back.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent event) {browser.back();}});// Create the forward buttonbutton_forward = new Button(controls, SWT.PUSH);button_forward.setText("前進");button_forward.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent event) {browser.forward();}});// Create the refresh buttonbutton_refresh = new Button(controls, SWT.PUSH);button_refresh.setText("刷新");button_refresh.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent event) {browser.refresh();}});// Create the stop buttonbutton_stop = new Button(controls, SWT.PUSH);button_stop.setText("停止");button_stop.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent event) {browser.stop();}});// Create the address entry field and set focus to iturl = new Combo(controls, SWT.ARROW_DOWN);url.setLayoutData(new GridData(296, SWT.DEFAULT));url.setSize(600, url.getSize().y);url.setFocus();// Create the go buttonbutton = new Button(controls, SWT.PUSH);button.setText("Go");button.addSelectionListener(new SelectionAdapter() {public void widgetSelected(SelectionEvent event) {String urlStr = url.getText();int flag = 1;browser.setUrl(urlStr);for (int i = 0; i < urlListItemCount; i++) {if (urlList[i].equals(urlStr)) {flag = 0;}}if (flag == 1) {urlList[urlListItemCount] = urlStr;url.add(urlStr);urlListItemCount++;}}});// Create the animated "throbber"throbber = new Label(controls, SWT.NONE);throbber.setText(AT_REST);// Allow users to hit enter to go to the typed URLshell.setDefaultButton(button);// Add event handlersbrowser.addCloseWindowListener(new AdvancedCloseWindowListener());browser.addLocationListener(new AdvancedLocationListener(url));browser.addProgressListener(new AdvancedProgressListener(throbber));browser.addStatusTextListener(new AdvancedStatusTextListener(status));// Go to the initial URLif (location != null) {browser.setUrl(location);}}/*** This class implements a CloseWindowListener for TestBrowser*/class AdvancedCloseWindowListener implements CloseWindowListener {/*** Called when the parent window should be closed*/public void close(WindowEvent event) {// Close the parent window((Browser) event.widget).getShell().close();}}/*** This class implements a LocationListener for TestBrowser*/class AdvancedLocationListener implements LocationListener {// The address text box to updateprivate Combo location;/*** Constructs an AdvancedLocationListener** @param text* the address text box to update*/public AdvancedLocationListener(Combo text) {// Store the address box for updateslocation = text;}/*** Called before the location changes** @param event* the event*/public void changing(LocationEvent event) {// Show the location that's loadinglocation.setText("加載 " + event.location + "...");}/*** Called after the location changes** @param event* the event*/public void changed(LocationEvent event) {// Show the loaded locationlocation.setText(event.location);}}/*** This class implements a ProgressListener for TestBrowser*/class AdvancedProgressListener implements ProgressListener {// The label on which to report progressprivate Label progress;/*** Constructs an AdvancedProgressListener** @param label* the label on which to report progress*/public AdvancedProgressListener(Label label) {// Store the label on which to report updatesprogress = label;}/*** Called when progress is made** @param event* the event*/public void changed(ProgressEvent event) {// Avoid divide-by-zeroif (event.total != 0) {// Calculate a percentage and display itint percent = (int) (event.current / event.total);progress.setText(percent + "%");} else {// Since we can't calculate a percent, show confusion :-)progress.setText("完成");}}/*** Called when load is complete** @param event* the event*/public void completed(ProgressEvent event) {// Reset to the "at rest" messageprogress.setText(AT_REST);}}/*** This class implements a StatusTextListener for TestBrowser*/class AdvancedStatusTextListener implements StatusTextListener {// The label on which to report statusprivate Label status;/*** Constructs an AdvancedStatusTextListener** @param label* the label on which to report status*/public AdvancedStatusTextListener(Label label) {// Store the label on which to report statusstatus = label;}/*** Called when the status changes** @param event* the event*/public void changed(StatusTextEvent event) {// Report the statusstatus.setText(event.text);}}/*** The application entry point** @param args* the command line arguments*/public static void main(String[] args) {new TestBrowser().run(args.length == 0 ? null : args[0]);} }
總結
以上是生活随笔為你收集整理的基于eclipse swt做java浏览器内嵌等功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何让Jtable不可编辑?(
- 下一篇: java访问远程共享文件