`

SWT学习笔记7——组件背景图片,鼠标事件,鼠标样式

 
阅读更多



import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;


/**
 * 测试SWT中鼠标事件的监听,组件背景颜色、背景图片的设置、鼠标样式的更改
 * @author LC
 *version: 2012_03_31
 */
public class TestComposite_Color_BackgroundImage_Mouse {
	static Display display = Display.getDefault();

	//三种获取鼠标的方式,第三种为自定义
	static Cursor handCursor=new Cursor(Display.getDefault(), SWT.CURSOR_HAND);
	static Cursor crossCursor=display.getSystemCursor(SWT.CURSOR_CROSS);
	static Cursor myCursor=new Cursor(display, new ImageData("hu.jpg"), 10, 10);
	
	public static void main(String[] args) {
		final Shell shell = new Shell();
		shell.setImage(SWTResourceManager.getImage("hu.jpg"));
		shell.setSize(450, 300);
		shell.setText("测试SWT组件的颜色、背景图片、鼠标样式");
		
		//组件一
		Composite composite = new Composite(shell, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION));//设置背景颜色
		composite.setBounds(10, 24, 113, 118);
//		composite.addMouseMoveListener(new MouseMoveListener() {
//			@Override
//			public void mouseMove(MouseEvent e) {
//				System.out.println("MouseMove:\n\t"+e);				
//			}
//		});
			
		//鼠标事件监听
		composite.addMouseTrackListener(new MouseTrackListener() {
			
			@Override
			public void mouseHover(MouseEvent e) {
				System.out.println("MouseHover:\n\t"+e);				
			}
			
			@Override
			public void mouseExit(MouseEvent e) {
				shell.setCursor(display.getSystemCursor(SWT.CURSOR_ARROW));//设置鼠标样式用该函数
				System.out.println("MouseExit:\n\t"+e);				
				
			}
			
			@Override
			public void mouseEnter(MouseEvent e) {
				shell.setCursor(myCursor);
				System.out.println("MouseEnter:\n\t"+e);				
				
			}
		});
		//组件二
		final Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setBackgroundImage(SWTResourceManager.getImage("hu.jpg"));//设置背景图片
		composite_1.setBounds(151, 24, 190, 199);
		
		Button button = new Button(composite_1, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(composite_1.getCursor()!=handCursor)
				composite_1.setCursor(handCursor);
				else composite_1.setCursor(crossCursor);
				
			}
		});
		button.setBounds(53, 98, 108, 27);
		button.setText("点我切换鼠标样式");

		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}
  • 大小: 92 KB
分享到:
评论
1 楼 sam301 2013-01-20  
好用,不客气了,我拿走了。 

相关推荐

Global site tag (gtag.js) - Google Analytics