eclipse中按钮实现显示图像

Posted 邱海传

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);

}

}


























































以上是关于eclipse中按钮实现显示图像的主要内容,如果未能解决你的问题,请参考以下文章

使用 Eclipse 的 Android 应用程序 - 创建按钮数组

eclipse如何显示行数

Eclipse 导出的 Runnable JAR 不显示图像

Image Servlet 不想在浏览器(FireFox、IE ..)中显示图像,但在 Eclipse 浏览器中有效吗?

Applet 和 Swing 未在 Eclipse 中显示组件

在Eclipse WindowBuilder中向按钮添加图标