SWT游览器组件的例子

来源:互联网 发布:手游cfbug软件 编辑:程序博客网 时间:2024/04/29 14:16

因为里面还有布局管理的代码,记录下来为后来参考

package com.swt;import org.eclipse.swt.SWT;import org.eclipse.swt.browser.Browser;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.events.TraverseEvent;import org.eclipse.swt.events.TraverseListener;import org.eclipse.swt.layout.GridData;import org.eclipse.swt.layout.GridLayout;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;import org.eclipse.swt.widgets.Text;public class SWTBrower {private static String DEFAULT_URL = "www.baidu.com";public static void main(String[] args) {Display display = new Display();display.setAppName("ddd");Shell shell = new Shell(display);shell.setText("SWT 自带游览器组件演示demo");GridLayout gl = new GridLayout(1, false);shell.setLayout(gl);GridData shellData = new GridData(GridData.FILL_BOTH);shellData.grabExcessHorizontalSpace = true;shellData.horizontalAlignment = SWT.FILL;shell.setLayoutData(shellData);// com1 用来放置文本输入框和按钮Composite com1 = new Composite(shell, SWT.NONE);com1.setLayout(new GridLayout(2, false));GridData com1Data = new GridData(GridData.FILL_HORIZONTAL);com1.setLayoutData(com1Data);final Text url = new Text(com1, SWT.BORDER);url.setText(DEFAULT_URL);GridData urlData = new GridData(GridData.FILL_HORIZONTAL);// urlData.horizontalAlignment = GridData.FILL;// urlData.verticalAlignment = GridData.FILL;url.setLayoutData(urlData);Button go = new Button(com1, SWT.PUSH);go.setText("Go");Composite com2 = new Composite(shell, SWT.NONE);com2.setLayout(new GridLayout(1, false));com2.setLayoutData(shellData);final Browser br = new Browser(com2, SWT.NONE);GridData gd = new GridData(GridData.FILL_BOTH);gd.grabExcessHorizontalSpace = true;gd.horizontalAlignment = SWT.FILL;br.setLayoutData(gd);br.setUrl(DEFAULT_URL);go.addSelectionListener(new SelectionAdapter() {@Overridepublic void widgetSelected(SelectionEvent e) {String s = url.getText();if (url == null || "".equals(url)) {} elsebr.setUrl(s);}});url.addTraverseListener(new TraverseListener() {public void keyTraversed(TraverseEvent e) {// 判断回车键if (e.keyCode == 13) {String s = url.getText();if (url == null || "".equals(url)) {} elsebr.setUrl(s);}}});shell.open();while (!shell.isDisposed()) {if (!display.readAndDispatch())display.sleep();}display.dispose();}}

运行的效果:


0 0
原创粉丝点击