eclipse中按钮实现显示图像
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import swing2swt.layout.FlowLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.graphics.*;  //加载图像需要用的包
public class Ima {
	protected Shell shell;
	private Device display;
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Ima window = new Ima();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(685, 493);
		shell.setText("SWT Application");
		shell.setLayout(new FormLayout());
		
		Composite composite = new Composite(shell, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.bottom = new FormAttachment(0, 436);
		fd_composite.right = new FormAttachment(0, 657);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setText("按钮");
		
		Image image=new Image(display,"C:\\Users\\lenovo\\Desktop\\8.jpg");
		image.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
		btnNewButton.setImage(image);
}
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号