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);

}

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM