SWT学习笔记3——颜色、字体、图片


SWT学习笔记3——颜色、字体、图片


import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Button;

public class TestColorImageFont {
	static Display display = Display.getDefault();
	//三种颜色获取方式
	static Color white = new Color(display, 255, 255, 255);
	static Color sysBlack = display.getSystemColor(SWT.COLOR_BLACK);
	static Color swtBlue = SWTResourceManager
			.getColor(SWT.COLOR_LIST_SELECTION);

	//两种图片获取方式,外加自己生成图片自己写数据
	static Image errorIcon = display.getSystemImage(SWT.ICON_ERROR); //内置图像
	static Image tigerIcon = new Image(display, "hu.jpg");//读取的图像
	//自己生成图像,参数为 图像宽、高,颜色的depth,RGB的掩膜(RGB对应的bit)
	static ImageData imageData=new ImageData(20, 20, 24, new PaletteData(0xFF0000, 0xFF00, 0xFF));
	static Image createdIcon=new Image(display, imageData);//难道是独立的?设置完数据重新生成才有效!!!否则是黑的而且直接设置图片的内容也不行!!!
	
	
	//字体
	static Font sysFont = display.getSystemFont();
	static Font songFont = new Font(display, "宋体", 22, SWT.NONE);

	public static void main(String[] args) {
		
		//设置生成的图像的内容
		for (int x = 0; x < imageData.width; x++) {
			for (int y = 0; y < imageData.height; y++) {
					imageData.setPixel(x, y, 0xFF00FF);
//					createdIcon.getImageData().setPixel(x, y, 0xFF00FF);//没用啊!!!
			}
		}
		createdIcon=new Image(display, imageData);//不加此行改了图片数据也无效啊!!!
		
		Shell shell = new Shell();
		shell.setSize(576, 226);
		shell.setText("SWT Application");
		shell.setBackground(sysBlack);
		shell.setImage(display.getSystemImage(SWT.ICON_WORKING));

		Label lblErr = new Label(shell, SWT.NONE);
		lblErr.setForeground(white);//前景颜色
		lblErr.setBackground(swtBlue);//背景颜色
		lblErr.setBounds(10, 10, 78, 32);
		//lblNewLabel.setImage(errorIcon); //label中文字和图片不能同时显示
		lblErr.setText("Error Label");
		lblErr.setToolTipText("label中文字和图片不能同时显示");

		Button btn = new Button(shell, SWT.NONE);
		btn.setAlignment(SWT.RIGHT);
		btn.setBounds(94, 31, 462, 127);
		btn.setText("Button Text");
		btn.setImage(tigerIcon);//图片
		btn.setFont(songFont);//字体

		
		Label lblCreate=new Label(shell, SWT.NONE);
		lblCreate.setAlignment(SWT.CENTER);
		lblCreate.setBounds(10, 100, 78, 32);
		lblCreate.setImage(createdIcon);
		
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}

你可能感兴趣的:(学习笔记)