锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
http://www.eclipse.org/articles/Article-Understanding-Layouts/Understanding-Layouts.htm
聽聽DropTarget target = new DropTarget(label, operations);
聽聽target.setTransfer(types);
聽聽target.addDropListener (new DropTargetAdapter() {
聽聽聽public void drop(DropTargetEvent event) {
聽聽聽聽if (event.data == null) {
聽聽聽聽聽event.detail = DND.DROP_NONE;
聽聽聽聽聽return;
聽聽聽聽}
聽聽聽聽label.setText ((String) event.data);
聽聽聽}
聽聽});
聽}
2銆侀殢鎰忔嫋鎷絋ree瀛愯妭鐐廣?br />
聽public static void main (String [] args) {
聽聽
聽聽final Display display = new Display ();
聽聽final Shell shell = new Shell (display);
聽聽shell.setLayout(new FillLayout());
聽聽//寤虹珛鐢ㄤ簬鎷栨嫿鐨勬爲(wèi)
聽聽final Tree tree = new Tree(shell, SWT.BORDER);
聽聽for (int i = 0; i < 3; i++) {
聽聽聽TreeItem item = new TreeItem(tree, SWT.NONE);
聽聽聽item.setText("item "+i);
聽聽聽for (int j = 0; j < 3; j++) {
聽聽聽聽TreeItem subItem = new TreeItem(item, SWT.NONE);
聽聽聽聽subItem.setText("item "+i+" "+j);
聽聽聽聽for (int k = 0; k < 3; k++) {
聽聽聽聽聽TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
聽聽聽聽聽subsubItem.setText("item "+i+" "+j+" "+k);
聽聽聽聽}
聽聽聽}
聽聽}
聽聽
聽聽Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
聽聽int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
聽聽
聽聽//寤虹珛鎷栨嫿 婧愨斺斺斺旀爲(wèi)tree瀵硅薄
聽聽final DragSource source = new DragSource (tree, operations);
聽聽source.setTransfer(types);
聽聽final TreeItem[] dragSourceItem = new TreeItem[1];
聽聽source.addDragListener (new DragSourceListener () {
聽聽聽public void dragStart(DragSourceEvent event) {
聽聽聽聽TreeItem[] selection = tree.getSelection();聽// 鑾峰彇鎵閫塼ree鐨勮妭鐐?br />聽聽聽聽if (selection.length > 0 && selection[0].getItemCount() == 0) //鍒ゆ柇鎵閫夎妭鐐規(guī)槸鍚﹀瓨鍦ㄥ茍涓斾負(fù)鏈鑺傜偣
聽聽聽聽{
聽聽聽聽聽event.doit = true;聽//鍚姩鎷栨嫿鍔熻兘
聽聽聽聽聽dragSourceItem[0] = selection[0]; //淇濆瓨鎷栨嫿鑺傜偣淇℃伅
聽聽聽聽} else {
聽聽聽聽聽event.doit = false;
聽聽聽聽}
聽聽聽};
聽聽聽public void dragSetData (DragSourceEvent event) {
聽聽聽聽event.data = dragSourceItem[0].getText(); //淇濆瓨鎷栨嫿鑺傜偣淇℃伅鍒頒簨浠舵暟鎹腑
聽聽聽}
聽聽聽public void dragFinished(DragSourceEvent event) {
聽聽聽聽if (event.detail == DND.DROP_MOVE)
聽聽聽聽聽dragSourceItem[0].dispose();
聽聽聽聽聽dragSourceItem[0] = null;
聽聽聽}
聽聽});
聽聽
聽聽//寤虹珛鎷栨嫿 鐩殑鈥斺斺斺旀爲(wèi)tree瀵硅薄
聽聽DropTarget target = new DropTarget(tree, operations);
聽聽target.setTransfer(types);
聽聽target.addDropListener (new DropTargetAdapter() {
聽聽聽//澶勭悊鎷栨嫿鍒扮洰鐨勪笂絀烘椂鐨勬柟娉?br />聽聽聽public void dragOver(DropTargetEvent event) {
聽聽聽聽event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL; //鍝嶅簲鎷栨嫿鍔ㄤ綔錛孎EEDBACK_EXPAND琛ㄧず鎷栨嫿鍒板彲浠ュ睍寮鐨勮妭鐐規(guī)椂灞曞紑鐩稿簲鑺傜偣銆?br />聽聽聽聽if (event.item != null) {
聽聽聽聽聽TreeItem item = (TreeItem)event.item;
聽聽聽聽聽//澶勭悊鎷栨嫿鍔ㄤ綔鍝嶅簲錛屾牴鎹嫋鎷界殑浣嶇疆涓嶅悓浜х敓涓嶅悓鐨勫搷搴?br />聽聽聽聽聽Point pt = display.map(null, tree, event.x, event.y); //鑾峰彇鎷栨嫿褰撳墠浣嶇疆鐐?br />聽聽聽聽聽Rectangle bounds = item.getBounds(); //鑾峰彇鐩殑鑺傜偣鐨勮竟妗?br />聽聽聽聽聽
聽聽聽聽聽//鏍規(guī)嵁鎷栨嫿褰撳墠浣嶇疆鐐逛笌鐩殑鑺傜偣鐨勮竟妗嗕綅緗緗搷搴旓紝濡傛樉紺轟負(fù)FEEDBACK_INSERT_BEFORE銆丗EEDBACK_INSERT_AFTER
聽聽聽聽聽if (pt.y < bounds.y + bounds.height/3) {
聽聽聽聽聽聽event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
聽聽聽聽聽} else if (pt.y > bounds.y + 2*bounds.height/3) {
聽聽聽聽聽聽event.feedback |= DND.FEEDBACK_INSERT_AFTER;
聽聽聽聽聽} else {
聽聽聽聽聽聽event.feedback |= DND.FEEDBACK_SELECT;
聽聽聽聽聽}
聽聽聽聽}
聽聽聽}
聽聽聽//澶勭悊鏀句笅鏃剁殑鏂規(guī)硶
聽聽聽public void drop(DropTargetEvent event) {
聽聽聽聽if (event.data == null) {
聽聽聽聽聽event.detail = DND.DROP_NONE;
聽聽聽聽聽return;
聽聽聽聽}
聽聽聽聽String text = (String)event.data;
聽聽聽聽//濡傛灉鏀句笅鐨勪綅緗病鏈夎妭鐐癸紝鍒欐柊寤虹珛涓涓妭鐐癸紝浠ユ牴鑺傜偣涓虹埗鑺傜偣
聽聽聽聽if (event.item == null)
聽聽聽聽{
聽聽聽聽聽TreeItem item = new TreeItem(tree, SWT.NONE);
聽聽聽聽聽item.setText(text);
聽聽聽聽}
聽聽聽聽//濡傛灉鏀句笅鐨勪綅緗湁鑺傜偣
聽聽聽聽else
聽聽聽聽{
聽聽聽聽聽TreeItem item = (TreeItem)event.item;
聽聽聽聽聽Point pt = display.map(null, tree, event.x, event.y);
聽聽聽聽聽Rectangle bounds = item.getBounds();
聽聽聽聽聽TreeItem parent = item.getParentItem();
聽聽聽聽聽if (parent != null) {
聽聽聽聽聽聽TreeItem[] items = parent.getItems();
聽聽聽聽聽聽int index = 0;
聽聽聽聽聽聽for (int i = 0; i < items.length; i++) {
聽聽聽聽聽聽聽if (items[i] == item) {
聽聽聽聽聽聽聽聽index = i;
聽聽聽聽聽聽聽聽break;
聽聽聽聽聽聽聽}
聽聽聽聽聽聽}
聽聽聽聽聽聽if (pt.y < bounds.y + bounds.height/3) {
聽聽聽聽聽聽聽//浠ュ綋鍓嶈妭鐐圭殑鐖惰妭鐐逛負(fù)鐖跺緩绔嬩竴涓瓙鑺傜偣錛屽茍涓旀彃鍏ュ埌褰撳墠鑺傜偣鍓?br />聽聽聽聽聽聽聽TreeItem newItem = new TreeItem(parent, SWT.NONE, index);
聽聽聽聽聽聽聽newItem.setText(text);
聽聽聽聽聽聽} else if (pt.y > bounds.y + 2*bounds.height/3) {
聽聽聽聽聽聽聽//浠ュ綋鍓嶈妭鐐圭殑鐖惰妭鐐逛負(fù)鐖跺緩绔嬩竴涓瓙鑺傜偣錛屽茍涓旀彃鍏ュ埌褰撳墠鑺傜偣鍓?br />聽聽聽聽聽聽聽TreeItem newItem = new TreeItem(parent, SWT.NONE, index+1);
聽聽聽聽聽聽聽newItem.setText(text);
聽聽聽聽聽聽} else {
聽聽聽聽聽聽聽//浠ュ綋鍓嶄負(fù)鐖惰妭鐐瑰緩绔嬩竴涓瓙鑺傜偣
聽聽聽聽聽聽聽TreeItem newItem = new TreeItem(item, SWT.NONE);
聽聽聽聽聽聽聽newItem.setText(text);
聽聽聽聽聽聽}
聽聽聽聽聽聽
聽聽聽聽聽} else {
聽聽聽聽聽聽TreeItem[] items = tree.getItems();
聽聽聽聽聽聽int index = 0;
聽聽聽聽聽聽for (int i = 0; i < items.length; i++) {
聽聽聽聽聽聽聽if (items[i] == item) {
聽聽聽聽聽聽聽聽index = i;
聽聽聽聽聽聽聽聽break;
聽聽聽聽聽聽聽}
聽聽聽聽聽聽}
聽聽聽聽聽聽if (pt.y < bounds.y + bounds.height/3) {
聽聽聽聽聽聽聽TreeItem newItem = new TreeItem(tree, SWT.NONE, index);
聽聽聽聽聽聽聽newItem.setText(text);
聽聽聽聽聽聽} else if (pt.y > bounds.y + 2*bounds.height/3) {
聽聽聽聽聽聽聽TreeItem newItem = new TreeItem(tree, SWT.NONE, index+1);
聽聽聽聽聽聽聽newItem.setText(text);
聽聽聽聽聽聽} else {
聽聽聽聽聽聽聽TreeItem newItem = new TreeItem(item, SWT.NONE);
聽聽聽聽聽聽聽newItem.setText(text);
聽聽聽聽聽聽}
聽聽聽聽聽}
聽聽聽聽聽
聽聽聽聽聽
聽聽聽聽}
聽聽聽}
聽聽});
聽聽shell.setSize (400, 400);
聽聽shell.open ();
聽聽while (!shell.isDisposed ()) {
聽聽聽if (!display.readAndDispatch ()) display.sleep ();
聽聽}
聽聽display.dispose ();
聽}
聽public static void main (String [] args) {
聽聽Display display = new Display ();
聽聽Shell shell = new Shell (display);
聽聽shell.setBounds (10, 10, 300, 200);
聽聽// create the composite that the pages will share
聽聽final Composite contentPanel = new Composite (shell, SWT.BORDER);
聽聽contentPanel.setBounds (100, 10, 190, 90);
聽聽final StackLayout layout = new StackLayout ();
聽聽contentPanel.setLayout (layout);
聽聽// create the first page's content
聽聽Composite page0;
聽聽page0 = new Composite (contentPanel, SWT.NONE);
聽聽page0.setLayout (new RowLayout ());
聽聽Label label = new Label (page0, SWT.NONE);
聽聽label.setText ("Label on page 1");
聽聽label.pack ();
聽聽// create the second page's content聽
聽聽Composite page1;
聽聽page1 = new Composite (contentPanel, SWT.NONE);
聽聽page1.setLayout (new RowLayout ());
聽聽Button button = new Button (page1, SWT.NONE);
聽聽button.setText ("Button on page 2");
聽聽button.pack ();
聽聽// create the button that will switch between the pages
聽聽Button pageButton = new Button (shell, SWT.PUSH);
聽聽pageButton.setText ("Push");
聽聽pageButton.setBounds (10, 10, 80, 25);
聽聽pageButton.addListener (SWT.Selection, new Listener () {
聽聽聽public void handleEvent (Event event) {
聽聽聽聽pageNum = ++pageNum % 2;
聽聽聽聽contentPanel.layout ();
聽聽聽}
聽聽});
聽聽layout.topControl = page0;
聽聽shell.open ();
聽聽while (!shell.isDisposed ()) {
聽聽聽if (!display.readAndDispatch ()) display.sleep ();
聽聽}
聽聽display.dispose ();
聽}
聽public static void main(String[] args) {
聽聽final Display display = new Display();
聽聽final Shell shell = new Shell(display);
聽聽shell.setText("SWT and Swing/AWT Example");
聽聽Listener exitListener = new Listener() {
聽聽聽public void handleEvent(Event e) {
聽聽聽聽MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.CANCEL | SWT.ICON_QUESTION);
聽聽聽聽dialog.setText("Question");
聽聽聽聽dialog.setMessage("Exit?");
聽聽聽聽if (e.type == SWT.Close) e.doit = false;
聽聽聽聽if (dialog.open() != SWT.OK) return;
聽聽聽聽shell.dispose();
聽聽聽}
聽聽};聽
聽聽Listener aboutListener = new Listener() {
聽聽聽public void handleEvent(Event e) {
聽聽聽聽final Shell s = new Shell(shell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
聽聽聽聽s.setText("About");
聽聽聽聽GridLayout layout = new GridLayout(1, false);
聽聽聽聽layout.verticalSpacing = 20;
聽聽聽聽layout.marginHeight = layout.marginWidth = 10;
聽聽聽聽s.setLayout(layout);
聽聽聽聽Label label = new Label(s, SWT.NONE);
聽聽聽聽label.setText("SWT and AWT Example.");
聽聽聽聽Button button = new Button(s, SWT.PUSH);
聽聽聽聽button.setText("OK");
聽聽聽聽GridData data = new GridData();
聽聽聽聽data.horizontalAlignment = GridData.CENTER;
聽聽聽聽button.setLayoutData(data);
聽聽聽聽button.addListener(SWT.Selection, new Listener() {
聽聽聽聽聽public void handleEvent(Event event) {
聽聽聽聽聽聽s.dispose();
聽聽聽聽聽}
聽聽聽聽});
聽聽聽聽s.pack();
聽聽聽聽Rectangle parentBounds = shell.getBounds();
聽聽聽聽Rectangle bounds = s.getBounds();
聽聽聽聽int x = parentBounds.x + (parentBounds.width - bounds.width) / 2;
聽聽聽聽int y = parentBounds.y + (parentBounds.height - bounds.height) / 2;
聽聽聽聽s.setLocation(x, y);
聽聽聽聽s.open();
聽聽聽聽while (!s.isDisposed()) {
聽聽聽聽聽if (!display.readAndDispatch()) display.sleep();
聽聽聽聽}
聽聽聽}
聽聽};
聽聽shell.addListener(SWT.Close, exitListener);
聽聽Menu mb = new Menu(shell, SWT.BAR);
聽聽MenuItem fileItem = new MenuItem(mb, SWT.CASCADE);
聽聽fileItem.setText("&File");
聽聽Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
聽聽fileItem.setMenu(fileMenu);
聽聽MenuItem exitItem = new MenuItem(fileMenu, SWT.PUSH);
聽聽exitItem.setText("&Exit\tCtrl+X");
聽聽exitItem.setAccelerator(SWT.CONTROL + 'X');
聽聽exitItem.addListener(SWT.Selection, exitListener);
聽聽MenuItem aboutItem = new MenuItem(fileMenu, SWT.PUSH);
聽聽aboutItem.setText("&About\tCtrl+A");
聽聽aboutItem.setAccelerator(SWT.CONTROL + 'A');
聽聽aboutItem.addListener(SWT.Selection, aboutListener);
聽聽shell.setMenuBar(mb);
聽聽RGB color = shell.getBackground().getRGB();
聽聽Label separator1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
聽聽Label locationLb = new Label(shell, SWT.NONE);
聽聽locationLb.setText("Location:");
聽聽Composite locationComp = new Composite(shell, SWT.EMBEDDED);
聽聽ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
聽聽ToolItem exitToolItem = new ToolItem(toolBar, SWT.PUSH);
聽聽exitToolItem.setText("&Exit");
聽聽exitToolItem.addListener(SWT.Selection, exitListener);
聽聽ToolItem aboutToolItem = new ToolItem(toolBar, SWT.PUSH);
聽聽aboutToolItem.setText("&About");
聽聽aboutToolItem.addListener(SWT.Selection, aboutListener);
聽聽Label separator2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
聽聽final Composite comp = new Composite(shell, SWT.NONE);
聽聽final Tree fileTree = new Tree(comp, SWT.SINGLE | SWT.BORDER);
聽聽Sash sash = new Sash(comp, SWT.VERTICAL);
聽聽Composite tableComp = new Composite(comp, SWT.EMBEDDED);聽//Composite瀵硅薄蹇呴』涓篠WT.EMBEDDED鎵嶅彲浠ヨ杞絘wt/swing鍐呭
聽聽Label separator3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
聽聽Composite statusComp = new Composite(shell, SWT.EMBEDDED);
聽聽java.awt.Frame locationFrame = SWT_AWT.new_Frame(locationComp);
聽聽final java.awt.TextField locationText = new java.awt.TextField();
聽聽locationFrame.add(locationText);
聽聽//澶勭悊awt琛?br />聽聽java.awt.Frame fileTableFrame = SWT_AWT.new_Frame(tableComp);
聽聽java.awt.Panel panel = new java.awt.Panel(new java.awt.BorderLayout());
聽聽fileTableFrame.add(panel);
聽聽final JTable fileTable = new JTable(new FileTableModel(null));
聽聽fileTable.setDoubleBuffered(true);
聽聽fileTable.setShowGrid(false);
聽聽fileTable.createDefaultColumnsFromModel();
聽聽JScrollPane scrollPane = new JScrollPane(fileTable);
聽聽panel.add(scrollPane);
聽聽//澶勭悊awt鐘舵丩abel
聽聽java.awt.Frame statusFrame = SWT_AWT.new_Frame(statusComp);
聽聽statusFrame.setBackground(new java.awt.Color(color.red, color.green, color.blue));
聽聽final java.awt.Label statusLabel = new java.awt.Label();
聽聽statusFrame.add(statusLabel);
聽聽statusLabel.setText("Select a file");
聽聽sash.addListener(SWT.Selection, new Listener() {
聽聽聽public void handleEvent(Event e) {
聽聽聽聽if (e.detail == SWT.DRAG) return;
聽聽聽聽GridData data = (GridData)fileTree.getLayoutData();
聽聽聽聽Rectangle trim = fileTree.computeTrim(0, 0, 0, 0);
聽聽聽聽data.widthHint = e.x - trim.width;
聽聽聽聽comp.layout();
聽聽聽}
聽聽});
聽聽File[] roots = File.listRoots();
聽聽for (int i = 0; i < roots.length; i++) {
聽聽聽File file = roots[i];
聽聽聽TreeItem treeItem = new TreeItem(fileTree, SWT.NONE);
聽聽聽treeItem.setText(file.getAbsolutePath());
聽聽聽treeItem.setData(file);
聽聽聽new TreeItem(treeItem, SWT.NONE);
聽聽}
聽聽fileTree.addListener(SWT.Expand, new Listener() {
聽聽聽public void handleEvent(Event e) {
聽聽聽聽TreeItem item = (TreeItem)e.item;
聽聽聽聽if (item == null) return;
聽聽聽聽if (item.getItemCount() == 1) {
聽聽聽聽聽TreeItem firstItem = item.getItems()[0];
聽聽聽聽聽if (firstItem.getData() != null) return;
聽聽聽聽聽firstItem.dispose();
聽聽聽聽} else {
聽聽聽聽聽return;
聽聽聽聽}
聽聽聽聽File root = (File)item.getData();
聽聽聽聽File[] files = root.listFiles();
聽聽聽聽if (files == null) return;
聽聽聽聽for (int i = 0; i < files.length; i++) {
聽聽聽聽聽File file = files[i];
聽聽聽聽聽if (file.isDirectory()) {
聽聽聽聽聽聽TreeItem treeItem = new TreeItem(item, SWT.NONE);
聽聽聽聽聽聽treeItem.setText(file.getName());
聽聽聽聽聽聽treeItem.setData(file);
聽聽聽聽聽聽new TreeItem(treeItem, SWT.NONE);
聽聽聽聽聽}
聽聽聽聽}
聽聽聽}
聽聽});
聽聽fileTree.addListener(SWT.Selection, new Listener() {
聽聽聽public void handleEvent(Event e) {
聽聽聽聽TreeItem item = (TreeItem)e.item;
聽聽聽聽if (item == null) return;
聽聽聽聽final File root = (File)item.getData();
聽聽聽聽EventQueue.invokeLater(new Runnable() {
聽聽聽聽聽public void run() {
聽聽聽聽聽聽statusLabel.setText(root.getAbsolutePath());
聽聽聽聽聽聽locationText.setText(root.getAbsolutePath());
聽聽聽聽聽聽fileTable.setModel(new FileTableModel(root.listFiles()));
聽聽聽聽聽}
聽聽聽聽});
聽聽聽}
聽聽});
聽聽
聽聽GridLayout layout = new GridLayout(4, false);
聽聽layout.marginWidth = layout.marginHeight = 0;
聽聽layout.horizontalSpacing = layout.verticalSpacing = 1;
聽聽shell.setLayout(layout);
聽聽GridData data;聽聽
聽聽data = new GridData(GridData.FILL_HORIZONTAL);
聽聽data.horizontalSpan = 4;
聽聽separator1.setLayoutData(data);
聽聽data = new GridData();
聽聽data.horizontalSpan = 1;
聽聽data.horizontalIndent = 10;
聽聽locationLb.setLayoutData(data);
聽聽data = new GridData(GridData.FILL_HORIZONTAL);
聽聽data.horizontalSpan = 2;
聽聽data.heightHint = locationText.getPreferredSize().height;
聽聽locationComp.setLayoutData(data);
聽聽data = new GridData(GridData.FILL_HORIZONTAL);
聽聽data.horizontalSpan = 1;
聽聽toolBar.setLayoutData(data);
聽聽data = new GridData(GridData.FILL_HORIZONTAL);
聽聽data.horizontalSpan = 4;
聽聽separator2.setLayoutData(data);
聽聽data = new GridData(GridData.FILL_BOTH);
聽聽data.horizontalSpan = 4;
聽聽comp.setLayoutData(data);
聽聽data = new GridData(GridData.FILL_HORIZONTAL);
聽聽data.horizontalSpan = 4;
聽聽separator3.setLayoutData(data);
聽聽data = new GridData(GridData.FILL_HORIZONTAL);
聽聽data.horizontalSpan = 4;
聽聽data.heightHint = statusLabel.getPreferredSize().height;
聽聽statusComp.setLayoutData(data);
聽聽
聽聽layout = new GridLayout(3, false);
聽聽layout.marginWidth = layout.marginHeight = 0;
聽聽layout.horizontalSpacing = layout.verticalSpacing = 1;
聽聽comp.setLayout(layout);聽聽聽
聽聽data = new GridData(GridData.FILL_VERTICAL);
聽聽data.widthHint = 200;
聽聽fileTree.setLayoutData(data);聽聽
聽聽data = new GridData(GridData.FILL_VERTICAL);
聽聽sash.setLayoutData(data);聽聽
聽聽data = new GridData(GridData.FILL_BOTH);
聽聽tableComp.setLayoutData(data);
聽聽shell.open();
聽聽while(!shell.isDisposed()) {
聽聽聽if (!display.readAndDispatch()) display.sleep();
聽聽}
聽聽display.dispose();
聽}
聽聽聽public void handleEvent(Event event)
聽聽聽{
聽聽聽聽switch (event.type)
聽聽聽聽{
聽聽聽聽聽case SWT.MouseMove :
聽聽聽聽聽聽if ((event.stateMask & SWT.BUTTON1) == 0)
聽聽聽聽聽聽聽break; // 鍒ゆ柇鏄惁涓洪紶鏍囧乏閿紝濡傛灉涓嶆槸璺沖嚭
聽聽聽聽聽聽GC gc = new GC(shell);
聽聽聽聽聽聽gc.drawLine(lastX, lastY, event.x, event.y);
聽聽聽聽聽聽gc.dispose();
聽聽聽聽聽// FALL THROUGH
聽聽聽聽聽case SWT.MouseDown :
聽聽聽聽聽聽lastX = event.x;
聽聽聽聽聽聽lastY = event.y;
聽聽聽聽聽聽break;
聽聽聽聽}
聽聽聽}
聽聽};
聽聽shell.addListener(SWT.MouseDown, listener);
聽聽shell.addListener(SWT.MouseMove, listener);
聽聽shell.open();
聽聽while (!shell.isDisposed())
聽聽{
聽聽聽if (!display.readAndDispatch())
聽聽聽聽display.sleep();
聽聽}
聽聽display.dispose();
聽}
2銆佸湪寮瑰嚭紿楀彛涓樉紺鴻〃鐨勫綋鏃跺浘鍍忕姸鎬併?br />聽public static void main(String[] args) {
聽聽final Display display = new Display();
聽聽final Shell shell = new Shell(display);
聽聽shell.setText("Widget");
聽聽
聽聽//寤虹珛涓涓畝鍗曠殑琛?br />聽聽final Table table = new Table(shell, SWT.MULTI);
聽聽table.setLinesVisible(true);
聽聽table.setBounds(10, 10, 100, 100);
聽聽for (int i = 0; i < 9; i++) {
聽聽聽new TableItem(table, SWT.NONE).setText("item" + i);
聽聽}
聽聽
聽聽//寤虹珛鎹曟崏鍥懼儚鐨勬寜閽?br />聽聽Button button = new Button(shell, SWT.PUSH);
聽聽button.setText("Capture");
聽聽button.pack();
聽聽button.setLocation(10, 140);
聽聽
聽聽
聽聽button.addListener(SWT.Selection, new Listener() {
聽聽聽public void handleEvent(Event event) {
聽聽聽聽Point tableSize = table.getSize();聽//鑾峰彇琛ㄧ殑澶у皬
聽聽聽聽GC gc = new GC(table);聽//寤虹珛琛ㄧ殑GC瀵硅薄
聽聽聽聽final Image image =
聽聽聽聽聽new Image(display, tableSize.x, tableSize.y);聽//寤虹珛琛ㄥぇ灝忕殑鍥懼儚image
聽聽聽聽gc.copyArea(image, 0, 0);聽//鍒╃敤琛ㄧ殑GC瀵硅薄鎶婅〃鐨勫浘鍍忓鍒跺埌image涓?br />聽聽聽聽gc.dispose();
聽聽聽聽
聽聽聽聽//寤虹珛涓涓脊鍑洪潰鏉縎hell瀵硅薄popup
聽聽聽聽Shell popup = new Shell(shell);
聽聽聽聽popup.setText("Image");
聽聽聽聽popup.addListener(SWT.Close, new Listener() {
聽聽聽聽聽public void handleEvent(Event e) {
聽聽聽聽聽聽image.dispose();
聽聽聽聽聽}
聽聽聽聽});
聽聽聽聽//鍦╬opup涓婂緩绔嬬敾甯冨璞anvas
聽聽聽聽Canvas canvas = new Canvas(popup, SWT.NONE);
聽聽聽聽canvas.setBounds(10, 10, tableSize.x+10, tableSize.y+10);
聽聽聽聽canvas.addPaintListener(new PaintListener() {
聽聽聽聽聽public void paintControl(PaintEvent e) {
聽聽聽聽聽聽e.gc.drawImage(image, 0, 0);聽//鍦ㄧ敾甯冧笂緇樺嚭琛ㄧ殑鍥懼儚image
聽聽聽聽聽}
聽聽聽聽});
聽聽聽聽popup.pack();
聽聽聽聽popup.open();
聽聽聽}
聽聽});
聽聽shell.pack();
聽聽shell.open();
聽聽while (!shell.isDisposed()) {
聽聽聽if (!display.readAndDispatch()) display.sleep();
聽聽}
聽聽display.dispose();
聽}
3銆佽幏鍙栨暣涓獥鍙g殑鍥懼儚騫舵樉紺恒?br />聽public static void main(String[] args) {
聽聽final Display display = new Display();
聽聽final Shell shell = new Shell(display);
聽聽shell.setLayout(new FillLayout());
聽聽Button button = new Button(shell, SWT.PUSH);
聽聽button.setText("Capture");
聽聽button.addListener(SWT.Selection, new Listener() {
聽聽聽public void handleEvent(Event event) {
聽聽聽聽
聽聽聽聽/* Take the screen shot */
聽聽聽聽GC gc = new GC(display);
聽聽聽聽final Image image = new Image(display, display.getBounds());
聽聽聽聽gc.copyArea(image, 0, 0);
聽聽聽聽gc.dispose();
聽聽聽聽
聽聽聽聽Shell popup = new Shell(shell, SWT.SHELL_TRIM);
聽聽聽聽popup.setLayout(new FillLayout());
聽聽聽聽popup.setText("Image");
聽聽聽聽popup.setBounds(50, 50, 200, 200);
聽聽聽聽popup.addListener(SWT.Close, new Listener() {
聽聽聽聽聽public void handleEvent(Event e) {
聽聽聽聽聽聽image.dispose();
聽聽聽聽聽}
聽聽聽聽});
聽聽聽聽
聽聽聽聽ScrolledComposite sc = new ScrolledComposite (popup, SWT.V_SCROLL | SWT.H_SCROLL);
聽聽聽聽Canvas canvas = new Canvas(sc, SWT.NONE);
聽聽聽聽sc.setContent(canvas);
聽聽聽聽canvas.setBounds(display.getBounds ());
聽聽聽聽canvas.addPaintListener(new PaintListener() {
聽聽聽聽聽public void paintControl(PaintEvent e) {
聽聽聽聽聽聽e.gc.drawImage(image, 0, 0);
聽聽聽聽聽}
聽聽聽聽});
聽聽聽聽popup.open();
聽聽聽}
聽聽});
聽聽shell.pack();
聽聽shell.open();
聽聽while (!shell.isDisposed()) {
聽聽聽if (!display.readAndDispatch()) display.sleep();
聽聽}
聽聽display.dispose();
聽}
4銆佷嬌鐢╰ransform銆乤lpha鍜宲aths娣峰悎鎶鏈粯鍥俱傛敞鎰忥細(xì)蹇呴』鍦ㄩ」鐩腑import鈥渟wt-gdip-win32-3139.dll鈥濄?br />聽public static void main(String[] args) {
聽聽final Display display = new Display();
聽聽final Shell shell = new Shell(display);
聽聽shell.setText("Advanced Graphics");
聽聽FontData fd = shell.getFont().getFontData()[0];
聽聽final Font font = new Font(display, fd.getName(), 60, SWT.BOLD | SWT.ITALIC);
聽聽final Image image = new Image(display, 640, 480);
聽聽final Rectangle rect = image.getBounds();
聽聽GC gc = new GC(image);
聽聽gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
聽聽gc.fillOval(rect.x, rect.y, rect.width, rect.height);
聽聽gc.dispose();
聽聽shell.addListener(SWT.Paint, new Listener() {
聽聽聽public void handleEvent(Event event) {
聽聽聽聽GC gc = event.gc;聽聽聽聽
聽聽聽聽Transform tr = new Transform(display);
聽聽聽聽tr.translate(50, 120);
聽聽聽聽tr.rotate(-30);
聽聽聽聽gc.drawImage(image, 0, 0, rect.width, rect.height, 0, 0, rect.width / 2, rect.height / 2);
聽聽聽聽gc.setAlpha(100);
聽聽聽聽gc.setTransform(tr);
聽聽聽聽Path path = new Path(display);
聽聽聽聽path.addString("SWT", 0, 0, font);
聽聽聽聽gc.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
聽聽聽聽gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
聽聽聽聽gc.fillPath(path);
聽聽聽聽gc.drawPath(path);
聽聽聽聽tr.dispose();
聽聽聽聽path.dispose();
聽聽聽}聽聽聽
聽聽});
聽聽shell.setSize(shell.computeSize(rect.width / 2, rect.height / 2));
聽聽shell.open();
聽聽while (!shell.isDisposed()) {
聽聽聽if (!display.readAndDispatch())
聽聽聽聽display.sleep();
聽聽}
聽聽image.dispose();
聽聽font.dispose();
聽聽display.dispose();
聽}
5銆佸鍥懼儚榪涜鏃嬭漿銆?br />聽public static void main(String[] args) {
聽聽final Display display = new Display();
聽聽
聽聽final Image image = new Image(display, 110, 60);
聽聽GC gc = new GC(image);
聽聽Font font = new Font(display, "Times", 30, SWT.BOLD);
聽聽gc.setFont(font);
聽聽gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
聽聽gc.fillRectangle(0, 0, 110, 60);
聽聽gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
聽聽gc.drawText("SWT", 10, 10, true);
聽聽font.dispose();
聽聽gc.dispose();
聽聽
聽聽final Rectangle rect = image.getBounds();
聽聽Shell shell = new Shell(display);
聽聽shell.setText("Matrix Tranformations");
聽聽shell.setLayout(new FillLayout());
聽聽final Canvas canvas = new Canvas(shell, SWT.DOUBLE_BUFFERED);
聽聽canvas.addPaintListener(new PaintListener () {
聽聽聽public void paintControl(PaintEvent e) {聽
聽聽聽聽GC gc = e.gc;
聽聽聽聽gc.setAdvanced(true);
聽聽聽聽if (!gc.getAdvanced()){
聽聽聽聽聽gc.drawText("Advanced graphics not supported", 30, 30, true);
聽聽聽聽聽return;
聽聽聽聽}
聽聽聽聽
聽聽聽聽// Original image
聽聽聽聽int x = 30, y = 30;
聽聽聽聽gc.drawImage(image, x, y);
聽聽聽聽x += rect.width + 30;
聽聽聽聽
聽聽聽聽Transform transform = new Transform(display);
聽聽聽聽
聽聽聽聽// Note that the tranform is applied to the whole GC therefore
聽聽聽聽// the coordinates need to be adjusted too.
聽聽聽聽
聽聽聽聽// Reflect around the y axis.
聽聽聽聽transform.setElements(-1, 0, 0, 1, 0 ,0);
聽聽聽聽gc.setTransform(transform);
聽聽聽聽gc.drawImage(image, -1*x-rect.width, y);
聽聽聽聽
聽聽聽聽x = 30; y += rect.height + 30;
聽聽聽聽
聽聽聽聽// Reflect around the x axis.
聽聽聽聽transform.setElements(1, 0, 0, -1, 0, 0);
聽聽聽聽gc.setTransform(transform);
聽聽聽聽gc.drawImage(image, x, -1*y-rect.height);
聽聽聽聽
聽聽聽聽x += rect.width + 30;
聽聽聽聽
聽聽聽聽// Reflect around the x and y axes聽
聽聽聽聽transform.setElements(-1, 0, 0, -1, 0, 0);
聽聽聽聽gc.setTransform(transform);
聽聽聽聽gc.drawImage(image, -1*x-rect.width, -1*y-rect.height);
聽聽聽聽
聽聽聽聽x = 30; y += rect.height + 30;
聽聽聽聽
聽聽聽聽// Shear in the x-direction
聽聽聽聽transform.setElements(1, 0, -1, 1, 0, 0);
聽聽聽聽gc.setTransform(transform);
聽聽聽聽gc.drawImage(image, 300, y);
聽聽聽聽
聽聽聽聽// Shear in y-direction
聽聽聽聽transform.setElements(1, -1, 0, 1, 0, 0);
聽聽聽聽gc.setTransform(transform);
聽聽聽聽gc.drawImage(image, 150, 475);
聽聽聽聽
聽聽聽聽// Rotate by 45 degrees聽
聽聽聽聽float cos45 = (float)Math.cos(45);
聽聽聽聽float sin45 = (float)Math.sin(45);
聽聽聽聽transform.setElements(cos45, sin45, -sin45, cos45, 0, 0);
聽聽聽聽gc.setTransform(transform);
聽聽聽聽gc.drawImage(image, 350, 100);
聽聽聽聽
聽聽聽聽transform.dispose();
聽聽聽}
聽聽});
聽聽
聽聽shell.setSize(350, 550);
聽聽shell.open();
聽聽while (!shell.isDisposed()) {
聽聽聽if (!display.readAndDispatch())
聽聽聽聽display.sleep();
聽聽}
聽聽image.dispose();
聽聽display.dispose();
聽}
SWT鏄?/span>Eclipse鍥懼艦API鐨勫熀紜錛屾湰鑺傚皢綆鍗曚粙緇嶄竴涓?/span>SWT涓墍鍖呭惈鐨勫瓙鍖呫?/span>
1銆?/span>org.eclipse.swt.widgets
鏈甯哥敤鐨勭粍浠跺熀鏈兘鍦ㄦ鍖呬腑錛屽Button銆?/span>Text銆?/span>Label銆?/span>Combo絳夈傚叾涓袱涓渶閲嶈鐨勭粍浠跺綋鏁?/span>Shell鍜?/span>Composite錛?/span>Shell鐩稿綋浜庡簲鐢ㄧ▼搴忕殑涓葷獥鍙o紱Composite鐩稿綋浜?/span>SWING涓殑Panel瀵硅薄錛屾槸瀹圭撼緇勪歡鐨勫鍣ㄣ?/span>
2銆?/span>org.eclipse.swt.layout
涓昏鐨勭晫闈㈠竷灞鏂瑰紡鍦ㄦ鍖呬腑銆?/span>SWT瀵圭粍浠剁殑甯冨眬涔熼噰鐢ㄤ簡AWT/SWING涓殑Layout鍜?/span>Layout Data緇撳悎鐨勬柟寮忋?/span>
3銆?/span>org.eclipse.swt.custom
瀵逛竴浜涘熀鏈浘褰㈢粍浠剁殑鎵╁睍鍦ㄦ鍖呬腑錛屾瘮濡傚叾涓殑CLabel灝辨槸瀵規(guī)爣鍑?/span>Label緇勪歡鐨勬墿灞曪紝鍦?/span>CLabel涓婂彲浠ュ悓鏃跺姞鍏ユ枃瀛楀拰鍥劇墖銆傚湪姝ゅ寘涓繕鏈変竴涓柊鐨勫竷灞鏂瑰紡StackLayout銆?/span>
4銆?/span>org.eclipse.swt.event
SWT閲囩敤浜嗗拰AWT/SWING涓鏍風(fēng)殑浜嬩歡妯″瀷錛屽湪鍖呬腑鍙互鎵懼埌浜嬩歡鐩戝惉綾誨拰鐩稿簲鐨勪簨浠跺璞°傛瘮濡傦紝榧犳爣浜嬩歡鐩戝惉鍣?/font>MouseListener錛?/span>MouseMoveListener絳夛紝鍙?qiáng)瀵瑰簲鐨勪簨錃g瀵硅薄MouseEvent銆?/span>
5銆?/span>org.eclipse.swt.graphics
姝ゅ寘涓寘鍚拡瀵瑰浘鐗囥佸厜鏍囥佸瓧浣撴垨緇樺浘API銆傛瘮濡傦紝鍙氳繃Image綾昏皟鐢ㄧ郴緇熶腑涓嶅悓綾誨瀷鐨勫浘鐗囨枃浠躲?/span>
6銆?/span>org.eclipse.swt.ole.win32
瀵逛笉鍚屽鉤鍙幫紝SWT鏈変竴浜涢拡瀵規(guī)х殑API銆備緥濡傦紝鍦?/span>Windows騫沖彴錛屽彲浠ラ氳繃姝ゅ寘寰堝鏄撶殑璋冪敤OLE緇勪歡錛岃繖浣垮緱SWT紼嬪簭涔熷彲浠ュ唴宓?/span>IE嫻忚鍣ㄦ垨Word銆?/span>Excel絳夌▼搴忋?/span>
姝ゅ榪樻湁org.eclipse.swt.dnd銆?/span>org.eclipse.swt.printing銆?/span>org.eclipse.swt.program銆?/span>org.eclipse.swt.accessibility銆?/span>org.eclipse.swt.browser銆?/span>org.eclipse.swt.awt絳夊寘錛屽湪姝や笉涓涓浠嬬粛浜嗐傝繖浜涘寘涓鑸緢灝戠敤鍒幫紝鍙渶瑕佺◢寰簡瑙d竴涓嬪氨琛屼簡錛屼笉蹇呮繁絀躲?br />
鐢?/strong>SWT Designer
聽聽聽SWT Designer鏄紭縐鐨?/span>SWT/JFace寮鍙戣緟鍔╁伐鍏?br />http://downloads.instantiations.com/Designer/release/v5.1.0_20060908125203/Designer_v5.1.0_win32_x86.exe
http://www.codefocus.cn/code/JAVA/SWT_Designer5.rar
codefocus.cn瑙e帇
聽聽聽SWT Designer榪樻棤娉曞畬鎴愭墍鏈夌殑鐣岄潰璁捐宸ヤ綔錛屾墍浠ュ湪鐣岄潰寮鍙戜腑渚濈劧鏄互鎵嬪伐鍐欎唬鐮佷負(fù)涓伙紝鑰屼笖鎵嬪啓浠g爜鏌愪簺鏃跺欐瘮鐣岄潰鎷栨媺鎿嶄綔鏇村揩鎹楓備互涓嬫槸絎旇呭湪浣跨敤SWT Designer寮鍙戠晫闈㈡椂鐨勫熀鏈祦紼嬶細(xì)
l聽聽聽聽聽聽聽聽聽聽 鏂板紑涓涓復(fù)鏃剁殑Application鏂囦歡錛岀敤SWT Desiger蹇熷仛濂藉紑鍙戞墍闇瑕佺殑閮ㄤ喚鐣岄潰銆?/span>
l聽聽聽聽聽聽聽聽聽聽 灝嗚嚜鍔ㄧ敓鎴愮殑浠g爜縐繪鍒版寮忛」鐩腑錛岃繘琛屾墜宸ヤ慨鏀瑰拰浠g爜綺劇畝銆?/span>
鍙﹀錛岀敱浜?/span>SWT Designer涓嶆槸寰堢ǔ瀹氾紝鎵浠ュ湪浣跨敤鏃惰繕搴旀敞鎰忥細(xì)
l聽聽聽聽聽聽聽聽聽聽 涓嶈鍦ㄧ晫闈腑鍔犲叆澶緇勪歡銆?/span>
l聽聽聽聽聽聽聽聽聽聽 涓嶈棰戠箒鐨勭Щ鍔ㄧ粍浠訛紝鎴栬呭垹闄ゅ張娣誨姞緇勪歡錛屽惁鍒欏緢鍙兘鍥犱負(fù)鍐呭瓨鑰楀敖鑰屾鏈恒?/span>