安装Jigloo插件的步骤在此省略。
首先建一个Java项目,命名为Converter。其效果是制作出一个图形界面,向其输入一段文字,可以将此段文字倒序显示出来。
建立一个java文件,代码如下:
package main; public class Converter { public static String doConvert(String input) { String output = ""; char[] input0 = input.toCharArray(); for (int i = input0.length - 1; i >= 0; i--) { output += input0[i]; } return output; } }
然后在创建一个SWT Composite。它的本质为java文件,不过可以使用Form Editor将其转化为图形化开发界面。
使用图形开发界面,设计出下图画布布局:
使用最终代码如下:
package main; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; /** * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI * Builder, which is free for non-commercial use. If Jigloo is being used * commercially (ie, by a corporation, company or business for any purpose * whatever) then you should purchase a license for each developer using Jigloo. * Please visit www.cloudgarden.com for details. Use of Jigloo implies * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR * ANY CORPORATE OR COMMERCIAL PURPOSE. */ public class NewComposite extends org.eclipse.swt.widgets.Composite { private Text text1; private Button OK; private Text text2; /** * Auto-generated main method to display this * org.eclipse.swt.widgets.Composite inside a new Shell. */ public static void main(String[] args) { showGUI(); } /** * Overriding checkSubclass allows this class to extend * org.eclipse.swt.widgets.Composite */ protected void checkSubclass() { } /** * Auto-generated method to display this org.eclipse.swt.widgets.Composite * inside a new Shell. */ public static void showGUI() { Display display = Display.getDefault(); Shell shell = new Shell(display); NewComposite inst = new NewComposite(shell, SWT.NULL); Point size = inst.getSize(); shell.setLayout(new FillLayout()); shell.layout(); if (size.x == 0 && size.y == 0) { inst.pack(); shell.pack(); } else { Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y); shell.setSize(shellBounds.width, shellBounds.height); } shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } public NewComposite(org.eclipse.swt.widgets.Composite parent, int style) { super(parent, style); initGUI(); } private void initGUI() { try { this.setLayout(null); this.setSize(251, 151); { text1 = new Text(this, SWT.NONE); text1.setBounds(48, 18, 161, 30); } { text2 = new Text(this, SWT.NONE); text2.setBounds(48, 71, 161, 30); } { OK = new Button(this, SWT.PUSH | SWT.CENTER); OK.setText("OK"); OK.setBounds(98, 113, 60, 30); OK.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent evt) { System.out.println("OK.widgetSelected, event="+evt); //TODO add your code for OK.widgetSelected text2.setText(Converter.doConvert(text1.getText()));//此为需要手动添加的代码 } }); } this.layout(); } catch (Exception e) { e.printStackTrace(); } } }
程序的运行结果如下图:
相信自己