`
sammor
  • 浏览: 413286 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

关于SWT的学习

    博客分类:
  • J2SE
阅读更多

 

     现在忙于毕业设计,需要有一些GUI界面。虽然自己对Swing比较熟悉,但Swing的界面,我还是无法控制好,做得像一个正常的软件界面出来了,有点儿难度。更不用谈说做到什么Swing一刀,Swing二刀的了(对我现在来说是浮云)。

 

    以前就知道eclipse是用java写的,但感觉还挺好看的,只是不知道怎么弄的,一开始还真以为是Swing做的,今天才发现是用SWT做出来的。

 

    查了一下SWT的相关信息,还跑了一些样例,发现还挺好看的。相对写起来应该会方便许多了。

 

    下面就列一下相关的学习信息吧。

 

   1、 SWT的官网:

         http://www.eclipse.org/swt/

 

   2、 对于初学者,非常好的学习网站:

         http://www.java2s.com/CN/Code/Java/SWT-JFace-Eclipse/CatalogSWT-JFace-Eclipse.htm

 

   3、SWT的补充控件(有挺多成品的东东,可以学习和借鉴哦) 

         http://www.eclipse.org/nebula/examples.php

 

 

1
2
分享到:
评论
14 楼 sammor 2011-02-16  
右击出现弹出菜单

this.addMenuDetectListener(new MenuDetectListener() {
			@Override
			public void menuDetected(MenuDetectEvent arg0) {
				Table table = (Table) arg0.widget;
				TableItem tableItem = table.getItem(table.getSelectionIndex());
				System.out.println(tableItem.getText(1));
				table.setMenu(createMenuPop());
			}
		});
13 楼 sammor 2011-02-16  
创建表格的弹出菜单

private static Menu createMenuPop() {
		final Menu menu = new Menu(FrontMainFrame.shell);
		MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
		menuItem.setText("MENU1");

		menu.addMenuListener(new MenuAdapter() {
			public void menuShown(MenuEvent e) {
				Table table = FrontMainFrame.rightComp.getTabItemErtuStatus()
						.getErtuStatusTable();
				Point p = FrontMainFrame.display.getCursorLocation();
				p = table.toControl(p);

				TableItem item = table.getItem(p);

				if (item == null) {
					menu.setVisible(false);
					return;
				}
			}
		});

		return menu;
	}
12 楼 sammor 2011-02-16  
如何为SWT Table添加列(Column)菜单

只有在右键单击某一列的单元格时,才在被选中的单元格上显示出该右键菜单。为实现这一需求,我们需要使用org.eclipse.swt.custom包中的TableCursor类,示例代码如下:

# public static void main(String[] args) {
#         Display display = new Display();
#         Shell shell = new Shell(display);
#         shell.setLayout(new GridLayout());
#

#         // create a a table with 3 columns and fill with data
#         final Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.SINGLE);
#         table.setLayoutData(new GridData(GridData.FILL_BOTH));
#         int columnSize = 3;
#         TableColumn column;
#         for (int i = 0; i < columnSize; i++) {
#             column = new TableColumn(table, SWT.NONE);
#             column.setWidth(100);
#             column.setText("Column" + i);
#             column.pack();
#         }
#         table.setHeaderVisible(true);
#         for (int i = 0; i < 10; i++) {
#             TableItem item = new TableItem(table, SWT.NONE);
#             item.setText(new String[] { "cell" + i + "0", "cell" + i + "1", "cell" + i + "2" });
#         }
#

#         final TableCursor cursor = new TableCursor(table, SWT.NONE);
#         cursor.setBackground(table.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
#         cursor.setForeground(table.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
#         cursor.setLayout(new FillLayout());
#         final Menu menu = new Menu(table);
#         MenuItem item = new MenuItem(menu, SWT.PUSH);
#         item.setText("Hello Menu");
#         cursor.addSelectionListener(new SelectionAdapter() {
#

#             public void widgetSelected(SelectionEvent e) {
#                 int column = cursor.getColumn();
#                 if (column == 1) {
#                     table.setMenu(menu);
#                 } else {
#                     table.setMenu(null);
#                 }
#             }
#         });
#

#         shell.open();
#         while (!shell.isDisposed()) {
#             if (!display.readAndDispatch())
#                 display.sleep();
#         }
#         display.dispose();
#     }
11 楼 sammor 2011-02-14  
点击按钮,使报文监视里面的Txt添加文本,有添加进去,却没有显示。

private static TaskTabFolder taskTabFold;
这里面也需要TaskTabFolder为静态,不明白是为什么,窗体已经是单例了。这个还需要加静态就不理解了。

public class ErtuTaskMainFrame {

	public static Display display = new Display();
	private Shell shell;

	private static TaskTabFolder taskTabFold;

	private static ErtuTaskMainFrame ertuTaskMainFrame = new ErtuTaskMainFrame();

	public static ErtuTaskMainFrame getInstance() {
		return ertuTaskMainFrame;
	}

	private ErtuTaskMainFrame() {
		initFrame();
	}

	private void initFrame() {
		shell = new Shell(display);
		FormLayout layout = new FormLayout();
		shell.setLayout(layout);
		shell.setText("任务调用模拟器");
		shell.setSize(700, 500);
		// 把窗体显示在显示中间
		initShell(display, shell);
		taskTabFold = new TaskTabFolder(shell, SWT.MULTI | SWT.BORDER);

		ErtuTimeStatusBar timeStatus = new ErtuTimeStatusBar(shell);
	}

	public void start() {
		shell.open();
		disposeFrame();
	}

	public void disposeFrame() {
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}

	/**
	 * 显示在屏幕中间
	 * 
	 * @param display
	 * @param shell
	 */
	private void initShell(Display display, Shell shell) {
		Monitor primary = display.getPrimaryMonitor();
		Rectangle bounds = primary.getBounds();
		Rectangle rect = shell.getBounds();
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		shell.setLocation(x, y);
	}

	public TaskTabFolder getTaskTabFold() {
		return taskTabFold;
	}

	public static void main(String[] args) {
		ErtuTaskMainFrame ertuFrame = new ErtuTaskMainFrame();
		ertuFrame.start();
	}
}
10 楼 sammor 2011-02-10  
清楚控件内容

@Override
			public void widgetSelected(SelectionEvent arg0) {
				TreeItem selectedItem = (TreeItem) arg0.item;

				RightCompt rightCompt = FrontMainFrame.rightComp;
				// 销毁之前的内容控件
				Control[] rightCs = rightCompt.getChildren();
				for (int i = 0; i < rightCs.length; i++) {
					rightCs[i].dispose();
				}

				if (selectedItem.getText().equals(ViewConfig.treeItemErtu)) {
					System.out.println("ss");
					CompErtuView compErtuView = new CompErtuView(rightCompt);
					rightCompt.layout();// 必须有,使重新刷新
				}

			}
9 楼 sammor 2011-02-10  
关于取子控件

Composite类有个getChildren()方法,可以得到Composite容器类的所有控件,如果容器类的控件都是Text,那么试一试
Composite   comp   =   new   Composite(shell,   SWT.NONE);
for   (int   i=0;   i <10;   i++)   {
    new   Text(comp,   SWT.BORDER).setText( "this   is   :   "+i);
}

Control[]   controls   =   compg.etChildren();

for   (int   i=0;   i <controls.length;   i++)   {
    if(controls[i]   instanceof   Text)   {
          Text   t   =   (Text)   controls[i];
          System.out.println(t.getText());
    }
} 


8 楼 sammor 2011-02-01  
时间控件

package view.ertuView.component;

import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TypedListener;

import view.ertuView.frame.ErtuMainFrame;

/**
 * The date picker panel
 * 
 * changes by sebthom ~ setDate will fire the Selection event + you can use
 * setDate(null) to clear the selection, the calendar will display the current
 * date, but getDate will return null until the user explicitely selects a date
 * from the control
 * 
 * @author 廖龙龙(修订)
 * @since 2008.12.9
 */
public class DatePicker extends Composite {

	// ~ Inner Classes
	// ----------------------------------------------------------
	private class DatePanel extends Canvas {
		// ~ Instance fields(实例域)
		// ----------------------------------------------------
		private int colSize;// 列宽
		private Display display = Display.getCurrent();
		private int rowSize;// 行高
		private Calendar temp = Calendar.getInstance();

		// 构造方法
		/**
		 * 使用GC绘图类实现日历面板
		 */
		public DatePanel(Composite parent, int style) {
			super(parent, style | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE);

			GC gc = new GC(this);
			Point p = gc.stringExtent("Q");
			gc.dispose();
			colSize = p.x * 3;// 列宽
			rowSize = (int) (p.y * 1.3);// 行高

			addPaintListener(new PaintListener() {
				public void paintControl(PaintEvent event) {
					onPaint(event);
				}
			});
			addControlListener(new ControlAdapter() {
				public void controlResized(ControlEvent e) {
					redraw();
				}
			});
			addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent e) {
					onKeyDown(e);
				}
			});
			addMouseListener(new MouseAdapter() {
				public void mouseDoubleClick(MouseEvent e) {
					doubleClick();
				}

				public void mouseDown(MouseEvent e) {
					onMouseDown(e);
				}
			});
			addMouseMoveListener(new MouseMoveListener() {
				public void mouseMove(MouseEvent e) {
					onMouseMove(e);
				}
			});
		}

		/**
		 * 根据星期几的位置顺序赋值,源代码这里有误
		 */
		private int computeOffset(int day) {
			switch (day) {
			case Calendar.MONDAY:
				return 2;
			case Calendar.TUESDAY:
				return 3;
			case Calendar.WEDNESDAY:
				return 4;
			case Calendar.THURSDAY:
				return 5;
			case Calendar.FRIDAY:
				return 6;
			case Calendar.SATURDAY:
				return 7;
			case Calendar.SUNDAY:
				return 1;
			}
			return -1;
		}

		public Point computeSize(int wHint, int hHint, boolean changed) {
			return new Point(colSize * 7, rowSize * 7);
		}

		/**
		 * Method drawTextImage.
		 * 
		 * @param gc
		 * @param string
		 * @param object
		 * @param i
		 * @param i1
		 * @param i2
		 * @param colSize
		 * @param rowSize
		 */
		private void drawTextImage(GC gc, String string, int x, int y,
				int colSize, int rowSize) {
			gc.fillRectangle(x, y, colSize, rowSize);
			gc.drawString(string, x, y, true);
		}

		private int getDayFromPoint(int x, int y) {
			int result = -1;

			for (int i = 1; i <= 31; i++) {
				Point p = getDayPoint(i);
				Rectangle r = new Rectangle(p.x, p.y, colSize, rowSize);
				if (r.contains(x, y)) {
					result = i;
					break;
				}
			}
			return result;
		}

		/**
		 * 设置星期几的标题样式
		 */
		private String getDayName(int day) {
			String weekdstr = dateSymbols.getShortWeekdays()[day];
			int weekindex = weekdstr.indexOf("星期");
			if (weekindex != -1) {
				weekdstr = weekdstr.substring(2);
			}
			return weekdstr;
		}

		/**
		 * 获取当前日期在表格中的位置坐标
		 */
		private Point getDayPoint(int day) {
			syncTime();
			temp.set(Calendar.DAY_OF_MONTH, 1);

			int firstDayOffset = computeOffset(temp.get(Calendar.DAY_OF_WEEK)) - 1;
			temp.set(Calendar.DAY_OF_MONTH, day);
			int dayOffset = computeOffset(temp.get(Calendar.DAY_OF_WEEK));
			int x = (dayOffset - 1) * colSize;
			// 设置首行日期数字开始显示的Y坐标
			int y = (1 + (((firstDayOffset + day) - 1) / 7)) * rowSize + 4;
			return new Point(x, y);
		}

		private int getMaxDay() {
			syncTime();

			int day = 28;

			for (int i = 0; i < 10; i++) {
				temp.set(Calendar.DAY_OF_MONTH, day);

				if (temp.get(Calendar.MONTH) != cal.get(Calendar.MONTH)) {
					return day - 1;
				}
				day++;
			}
			return -1;
		}

		private void onKeyDown(KeyEvent e) {
			if (e.character == SWT.ESC) {
				dateSelected(false);
				return;
			}

			if ((e.character == ' ') || (e.character == '\r')) {
				dateSelected(true);
				return;
			}

			int day = cal.get(Calendar.DAY_OF_MONTH);
			int month = cal.get(Calendar.MONTH);
			int oldDay = day;
			int oldMonth = month;

			if (e.keyCode == SWT.ARROW_LEFT) {
				day--;
			}

			if (e.keyCode == SWT.ARROW_RIGHT) {
				day++;
			}

			if (e.keyCode == SWT.ARROW_UP) {
				day = ((day - 7) < 1 ? oldDay : day - 7);
			}

			if (e.keyCode == SWT.ARROW_DOWN) {
				day = ((day + 7) > getMaxDay() ? oldDay : day + 7);
			}

			if (e.keyCode == SWT.PAGE_UP) {
				month--;
			}

			if (e.keyCode == SWT.PAGE_DOWN) {
				month++;
			}

			cal.set(Calendar.MONTH, month);
			cal.set(Calendar.DAY_OF_MONTH, day);

			if ((day != oldDay) || (month != oldMonth)) {
				redraw();

				if (month != oldMonth) {
					updateMonthLabel();
				}
			}
		}

		private void onMouseDown(MouseEvent e) {
			int day = getDayFromPoint(e.x, e.y);

			if (day > 0) {
				cal.set(Calendar.DAY_OF_MONTH, day);
				dateSelected(true);
				updateDate();
			}
		}

		private void onMouseMove(MouseEvent e) {
			int day = getDayFromPoint(e.x, e.y);
			selection = day;
			updateDate();
		}

		/**
		 * 绘制日期显示面板及其相应的日期数值
		 */
		private void onPaint(PaintEvent event) {
			Rectangle rect = getClientArea();
			GC gc0 = event.gc;
			Image image = new Image(display, rect.width, rect.height);
			GC gc = new GC(image);
			gc.setBackground(display
					.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
			gc.fillRectangle(rect);
			// 绘制面板的标题,显示星期几信息
			int x = 0;
			int y = 1;

			for (int i = 0; i < 7; i++) {
				// 将表头中星期六这一格显示为红色
				// if (i == 6) {
				// gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
				// }
				gc.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
				drawTextImage(gc, getDayName(1 + i), x, 3, colSize, rowSize);
				x += colSize;
			}

			gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
			y += rowSize;
			// 使用默认的前景色绘制星期几上下的直线
			gc.drawLine(0, 0 + 1, rect.width, 0 + 1);
			gc.drawLine(0, y - 1, rect.width, y - 1);
			// 在日期的最后画一条直线
			// gc.drawLine(0, y +rowSize*6, rect.width, y + rowSize*6);

			syncTime();

			int day = 1;

			while (true) {
				temp.set(Calendar.DAY_OF_MONTH, day);

				if (temp.get(Calendar.MONTH) != cal.get(Calendar.MONTH)) {
					break;
				}
				// 判断给定的日期是星期几
				// int dayOffset =
				// computeOffset(temp.get(Calendar.DAY_OF_WEEK));
				Point p = getDayPoint(day);
				// 根据当前系统的颜色设置日历面板的颜色
				if (day == cal.get(Calendar.DAY_OF_MONTH)) {
					gc.setForeground(display
							.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
					gc.setBackground(display
							.getSystemColor(SWT.COLOR_LIST_SELECTION));
				} else if (day == selection) {
					gc.setForeground(display
							.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
					gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
				} else {
					gc.setBackground(display
							.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
					// 如果是星期六的日期就显示为红色
					// gc.setForeground(display
					// .getSystemColor(dayOffset == 7 ? SWT.COLOR_RED
					// : SWT.COLOR_BLACK));
					gc.setForeground(display.getSystemColor(SWT.COLOR_BLACK));
				}

				drawTextImage(gc, "" + day, p.x, p.y, colSize, rowSize);
				day++;
			}

			gc0.drawImage(image, 0, 0);
			gc.dispose();
			image.dispose();
		}

		private void syncTime() {
			temp.setTimeInMillis(cal.getTimeInMillis());
		}
	}

	// ~ Instance fields
	// --------------------------------------------------------
	private Calendar cal = Calendar.getInstance();

	// sebthom
	private Date selectedDate;

	private DatePanel datePanel;
	private DateFormatSymbols dateSymbols = new DateFormatSymbols();
	private Label monthLabel;
	private int selection = -1;

	// ~ Constructors
	// -----------------------------------------------------------
	public DatePicker(Composite parent, int style) {
		super(parent, style);

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 5;
		gridLayout.verticalSpacing = gridLayout.horizontalSpacing = 0;
		gridLayout.marginHeight = gridLayout.marginWidth = 0;
		setBackground(ErtuMainFrame.display.getSystemColor(SWT.COLOR_WHITE));
		setLayout(gridLayout);

		GridData gridData;

		// previous year
		Button prevYear = new Button(this, SWT.FLAT);
		prevYear.setToolTipText("上一年");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.heightHint = gridData.widthHint = 19;
		prevYear.setLayoutData(gridData);
		prevYear.setText("<<");
		prevYear.setSelection(false);
		prevYear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				cal.roll(Calendar.YEAR, -1);
				updateDate();
			}
		});

		// previous month
		Button prevMonth = new Button(this, SWT.FLAT);
		prevMonth.setToolTipText("上一月");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		gridData.heightHint = gridData.widthHint = 19;
		prevMonth.setLayoutData(gridData);
		prevMonth.setText("<");
		prevMonth.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				cal.roll(Calendar.MONTH, -1);
				updateDate();
			}
		});

		// current month
		monthLabel = new Label(this, SWT.CENTER);
		monthLabel.setToolTipText("选择的日期");
		gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.CENTER);
		gridData.heightHint = prevYear.computeSize(19, 19).y;
		gridData.grabExcessHorizontalSpace = true;
		monthLabel.setLayoutData(gridData);

		// next month
		Button nextMonth = new Button(this, SWT.FLAT);
		nextMonth.setToolTipText("下一月");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gridData.heightHint = gridData.widthHint = 19;
		nextMonth.setLayoutData(gridData);
		nextMonth.setText(">");
		nextMonth.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				cal.roll(Calendar.MONTH, 1);
				updateDate();
			}
		});

		// next year
		Button nextYear = new Button(this, SWT.FLAT);
		nextYear.setToolTipText("下一年");
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gridData.heightHint = gridData.widthHint = 19;
		nextYear.setLayoutData(gridData);
		nextYear.setText(">>");
		nextYear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				cal.roll(Calendar.YEAR, 1);
				updateDate();
			}
		});

		// a panel
		datePanel = new DatePanel(this, SWT.NONE);
		gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalSpan = 5;
		datePanel.setLayoutData(gridData);

		updateDate();
	}

	// ~ Methods
	// ----------------------------------------------------------------
	public void addSelectionListener(SelectionListener listener) {
		checkWidget();

		if (listener == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		TypedListener typedListener = new TypedListener(listener);
		addListener(SWT.Selection, typedListener);
		addListener(SWT.DefaultSelection, typedListener);
	}

	public Point computeSize(int wHint, int hHint, boolean changed) {
		Point pSize = datePanel.computeSize(wHint, hHint, changed);
		Point labelSize = monthLabel.computeSize(wHint, hHint, changed);

		int x = (pSize.x < (labelSize.x + 80) ? labelSize.x + 80 : pSize.x);
		int y = (pSize.y < (labelSize.y + 20) ? labelSize.y + 20 : pSize.y);
		return new Point(x, y);
	}

	private void dateSelected(boolean good) {
		// sebthom
		if (good)
			selectedDate = cal.getTime();
		Event event = new Event();
		event.doit = good;
		notifyListeners(SWT.Selection, event);
	}

	private void doubleClick() {
		Event event = new Event();
		event.doit = true;
		notifyListeners(SWT.MouseDoubleClick, event);
	}

	private String getCurrentMonthName() {
		StringBuffer sb = new StringBuffer();
		sb.append(cal.get(Calendar.YEAR));
		sb.append("年");
		sb.append(getMonthName(cal.get(Calendar.MONTH)));
		sb.append("月");
		return sb.toString();
	}

	/**
	 * 获取当前选择的日期
	 */
	public Date getDate() {
		return selectedDate;
	}

	/**
	 * 设置中文缩减式日期显示 (1)十二月、十一月 (2)12月、11月
	 */
	private String getMonthName(int month) {
		// 设置月份显示样式为如十二月、十一月
		// String monname = dateSymbols.getShortMonths()[month];
		// System.out.println(month);
		// int endindex = monname.indexOf("月");
		// if (endindex != -1) {
		// monname = monname.substring(0, endindex);
		// }
		// 设置月份显示样式为如12月、11月
		String monname = String.valueOf(month + 1);
		return monname;
	}

	public void removeSelectionListener(SelectionListener listener) {
		checkWidget();

		if (listener == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		removeListener(SWT.Selection, listener);
		removeListener(SWT.DefaultSelection, listener);
	}

	public void reset() {
		cal = Calendar.getInstance();
		updateDate();
	}

	/**
	 * 初始化时,显示当前日期
	 */
	public void setDate(Date date) {
		// sebthom
		// cal.setTime(date);
		selectedDate = date;
		cal.setTime(selectedDate == null ? new Date() : date);

		updateMonthLabel();
		redraw();
	}

	private void updateDate() {
		datePanel.redraw();
		updateMonthLabel();
	}

	private void updateMonthLabel() {
		monthLabel.setText(getCurrentMonthName());
	}
}
7 楼 sammor 2011-02-01  
当用多线程在运行过程中,要调用到控件的时候
会出现
        Exception in thread "Timer-0" org.eclipse.swt.SWTException: Invalid thread access
这样的一个错误(线程无法调用,也只是因为我们在线程的run里面去调到了了控件的东西了)


下面我做了一个状态栏,里面有一个时间显示(一个定时程序去跑的)

package view.ertuView.component;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

import view.ertuView.frame.ErtuMainFrame;

/**
 * 前置机状态栏
 * 
 * @author Sammor
 * @date 2011-2-1
 */
public class ErtuStatusBar extends Menu {

	private Date ertuTime = new Date();

	private Label labTimeStatus;

	public ErtuStatusBar(Decorations parent) {
		super(parent, SWT.BAR);

		labTimeStatus = new Label(parent, SWT.BORDER);

		FormData labelData = new FormData();
		labelData.left = new FormAttachment(0);
		labelData.right = new FormAttachment(100);
		labelData.bottom = new FormAttachment(100);
		labTimeStatus.setLayoutData(labelData);

		statusInit();
	}

	public void statusInit() {
		Timer time = new Timer();
		time.schedule(new ErtuTimeStatus(), new Date(), 1000);

		// timeStatus = new MenuItem(this, SWT.PUSH);
		labTimeStatus.setText(ertuTime.toLocaleString());
	}

	

	public class ErtuTimeStatus extends TimerTask {

		public void run() {
			ertuTime.setTime(ertuTime.getTime() + 1000);
			ErtuMainFrame.display.asyncExec(new Runnable() {
				public void run() {
					setlabTimeStatus(ertuTime.toLocaleString());
				}
			});
		}
	}
	
	private void setlabTimeStatus(final String datetime) {
		labTimeStatus.setText(datetime);
	}

	protected void checkSubclass() {
	}
	
	public Date getErtuTime() {
		return ertuTime;
	}

	public void setErtuTime(Date ertuTime) {
		this.ertuTime = ertuTime;
	}

}






6 楼 sammor 2011-02-01  
SWT中Combo与CCombo控件的区别 (收藏)

插件开发时常会用到Combo和CCombo控件,以下是对两者区别的一点总结

1)SWT.SIMPLE样式不同,Combo支持SIMPLE样式,而CCombo不支持SIMPLE样式

2)当父控件使用FlowLayout布局管理器时,改变父控件大小,CCombo控件的高度会随之发生变化,而Combo则不会

3)弹出下拉列表动作的效果不同

4)SWT.READ_ONLY样式不同,默认CCombo背景呈灰色显示,且在READ_ONLY样式下,Combo.setText(...) 方法只能设置下拉列表中已存在值中的某一项,而CCombo.setText(...)方法可设置下拉列表中不存在的文本
5 楼 sammor 2011-01-30  
设置位置

	
                Rectangle rect = parent.getBounds();
		this.setBounds(0, 0, rect.width, rect.height);
		FormData formData = new FormData();
		formData.top = new FormAttachment(0, 0);
		formData.bottom = new FormAttachment(100, -3);
		formData.left = new FormAttachment(0, 0);
		// 第一个为百分比 ,第二个为移动的像素
		formData.right = new FormAttachment(100, -3);
		this.setLayoutData(formData)
;
4 楼 sammor 2011-01-30  
对于TabFolder中的内容添加,需要加上Panel一样的东西,这里面是composite

TabItem tabItemServer = new TabItem(this, SWT.NONE);
		tabItemServer.setText("服务");

		GridLayout gridlayout = new GridLayout();
		gridlayout.numColumns = 2;

		Composite composite_Server = new Composite(this, SWT.NONE);
		// 需要加上而已可以显示
		composite_Server.setLayout(gridlayout);

		composite_Server.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_GRAY));

		// Group tabServerGroup = new Group(composite_Server, SWT.NONE);
		// tabServerGroup.setLayout(gridlayout);
		tabItemServer.setControl(composite_Server);

		Button btnStartServer = new Button(composite_Server, SWT.PUSH);
		btnStartServer.setText("启动服务");

		Button btnStopServer = new Button(composite_Server, SWT.PUSH);
		btnStopServer.setText("停止服务");
3 楼 sammor 2011-01-30  

设置颜色
tabServerGroup.setBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_GRAY));
2 楼 sammor 2011-01-30  
TAB的相关例子
Display display = Display.getDefault();
		Shell shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");

		TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
		tabFolder.setBounds(10, 0, 422, 256);

		TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("New Item");

		Text text = new Text(tabFolder, SWT.BORDER);
		tabItem.setControl(text);

		TabItem tabItem_2 = new TabItem(tabFolder, SWT.NONE);
		tabItem_2.setText("New Item");

		Button button = new Button(tabFolder, SWT.NONE);
		tabItem_2.setControl(button);
		button.setText("New Button");

		TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("New Item");

		Label label = new Label(tabFolder, SWT.NONE);
		tabItem_1.setControl(label);
		label.setText("New Label");

		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
1 楼 sammor 2011-01-30  
如果需要写自定义的控件,会出现:
Exception in thread "main" org.eclipse.swt.SWTException: Subclassing not allowed

这样的错误.

需要在类加添加:
	protected void checkSubclass() {
	}

相关推荐

    swt入门学习资料

    错误,提示找不到相应的库。这是因为 SWT 库依赖于本地操作系统,需要对应操作系统的动态链接库(DLL)。对于Windows系统,需要 swt_win32.dll,而...通过学习 SWT,开发者可以创建出既美观又高效的跨平台应用程序。

    SWT编程开发学习资料

    在"SWT编程开发学习资料"中,你可以期待获取关于以下几个方面的深入理解和实践指导: 1. **SWT基础知识**:SWT提供了一系列控件,如按钮、文本框、列表、树、表格等,以及布局管理器,如FillLayout、GridLayout、 ...

    swt学习资料pdf

    在"swt学习资料pdf"中,我们可以期待涵盖以下几个关键知识点: 1. **SWT的起源和发展**:SWT的出现是为了克服Java AWT和Swing库在性能和平台原生性上的局限。通过直接调用操作系统API,SWT能够实现更高的性能和更好...

    swt - designer 学习实例

    这个"swt - designer 学习实例"压缩包显然包含了一系列关于如何使用SWT进行编程的示例,这对于学习和理解SWT的用法非常有帮助。SWT为Java开发者提供了与本机操作系统深度集成的用户界面组件,如按钮、文本框、列表等...

    SWT 案例之学习精华

    SWT,全称为Standard Widget ..."地址.txt"可能包含一些关于案例代码位置或使用说明的信息,而"WSTDemo.rar"很可能是包含实际SWT应用实例的压缩文件,解压后可以运行并分析代码,进一步巩固和提升你的SWT编程技能。

    swt tree学习文档

    ### SWT Tree 学习文档知识点总结 #### 一、SWT Tree 概念与应用场景 SWT (Standard Widget Toolkit) 是一种用于构建图形用户界面(GUI)的应用程序框架,广泛应用于Java开发环境中。SWT提供了丰富的组件库,使得...

    学习SWT,RCP的笔记.doc

    ### SWT与RCP基础知识 #### 一、SWT概述 SWT(Standard Widget Toolkit)是一种专为Java设计的用户界面工具包,它提供了一系列的原生控件来创建丰富的客户端应用。SWT的一个显著特点在于它能直接调用操作系统的GUI...

    swt-api swt DOC

    在这个案例中,"swt.CHM"很可能就是那个关于SWT API的详细手册,开发者可以通过它快速查找和学习SWT的各种函数、类和方法。 在实际开发中,SWT提供了一系列的控件,如按钮、文本框、列表、树视图等,以及布局管理器...

    SWT源码 swt.jar

    描述中提到的"src.zip"文件包含的是SWT的源代码,这对于开发者进行调试、学习和定制SWT行为至关重要。源代码可以被导入到任何支持Java开发的IDE,比如Eclipse,通过关联源码,可以在编写代码时直接查看SWT的内部实现...

    swtjface api.chm org.eclipse.swt是学习swt/jface帮助文档2

    swtjface api.chm org.eclipse.swt是学习swt/jface帮助文档2

    swt 开发学习

    在SWT开发学习中,首先需要理解SWT的基本概念和架构。SWT基于事件驱动模型,通过监听用户的交互操作来更新界面。它的核心类包括Display、Shell、Composite、Widget等,这些类构成了SWT的基础框架。Display代表整个...

    SWT相关学习资料(多个pdf文档)

    总的来说,这些学习资料提供了全面而深入的SWT学习路径,从基础到进阶,从理论到实践,对于想要提升Java GUI开发技能,特别是涉及Eclipse RCP开发的人员来说,是一份宝贵的资源。通过系统学习,开发者不仅可以掌握...

    swt 学习代码

    本学习代码着重关注SWT的基本使用和常见组件的实践。 在SWT中,开发者可以创建各种窗口、按钮、文本框、列表、树形视图等控件,它们是构建用户交互界面的基础。例如,`Shell`类代表一个顶级窗口,`Composite`类作为...

    Java swt完整教程

    Java SWT(Standard Widget Toolkit)是Java编程环境中用于创建桌面应用程序的一种图形用户界面(GUI)工具包。它是Eclipse基金会的一部分,提供了与操作系统本机UI组件紧密集成的能力,使得开发出的程序具有原生...

    SWT控件知识学习笔记

    ### SWT控件知识学习笔记 #### 一、SWT/JFace常用组件介绍 SWT (Standard Widget Toolkit) 是 Eclipse 平台的一个图形界面工具包,它提供了与平台无关的 GUI 组件,允许开发者构建高性能的应用程序。SWT/JFace ...

    swt 学习资料02

    在这个"swt 学习资料02"的压缩包中,我们关注的核心知识点是“定时器”和“键盘事件”。 1. **定时器(Timer)**: - **定时器原理**:定时器在Java GUI编程中用于在特定间隔执行某些任务。SWT提供了`Display....

    关于SWT的中文教程

    本教程的目的是帮助中文用户理解和掌握SWT的基本用法,解决在中文环境中寻找SWT学习资料的困难。以下是一些关键知识点的详细介绍: 1. **SWT控件**:SWT提供了一系列的基础控件,如Label(标签)、Button(按钮)、...

    Swt.JFace.in.Action 和 SWT 学习资料(两份资料)

    Swt.JFace.in.Action这本书是一本关于SWT和JFace实践应用的书籍,它详细介绍了如何使用这两个库来创建功能丰富的Java桌面应用。书中的内容可能包括: 1. **SWT基础**:介绍SWT的基本概念,如Widget、Shell、...

    swtjface api.chm org.eclipse.swt是学习swt/jface帮助文档1

    swtjface api.chm org.eclipse.swt是学习swt/jface帮助文档1

    swt高级密笈源码swt

    4. **对话框**:SWT提供了诸如消息对话框(MessageDialog)、输入对话框(InputDialog)和文件选择对话框(FileDialog)等,学习如何创建和使用这些对话框,可以增强应用的交互性。 5. **拖放操作**:SWT支持拖放...

Global site tag (gtag.js) - Google Analytics