用SWT如果实现图片显示 我想点击按钮后显示一张已经存在的JPG图片,请问要怎么做? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SWT不适合图像处理,用Swing吧。 import org.eclipse.swt.SWT;import org.eclipse.swt.events.PaintEvent;import org.eclipse.swt.events.PaintListener;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.graphics.Image;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Canvas;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.FileDialog;import org.eclipse.swt.widgets.Shell;public class MyImage { private String fileName = null; private Image image = null; private Canvas canvas; public static void main(String[] args) { try { MyImage window = new MyImage(); window.open(); } catch (Exception e) { e.printStackTrace(); } } public void open() { final Display display = Display.getDefault(); final Shell shell = new Shell(); shell.setSize(309, 314); shell.setText("SWT Application"); shell.open(); Button button_Scan = new Button(shell, SWT.NONE); button_Scan.setText("浏览"); button_Scan.setBounds(3, 3, 55, 25); button_Scan.addSelectionListener(new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { FileDialog dlg =new FileDialog(shell, SWT.OPEN); dlg.setText("Open"); dlg.setFilterNames(new String[]{"图片文件(*.jpg)","图片文件(*.gif)"}); dlg.setFilterExtensions(new String[]{"*.jpg", "*.gif"}); fileName = dlg.open();//取得打开图片的物理地址 if (fileName!=null){ image = new Image(null, fileName); canvas.redraw(); } } }); canvas = new Canvas(shell, SWT.BORDER); canvas.setBounds(0, 29, 300, 256); canvas.addPaintListener(new PaintListener(){ public void paintControl(PaintEvent arg0) { if (image != null) { arg0.gc.drawImage(image, 0, 0); } } }); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } }} 楼上正解,建议楼主碰到问题多查查apiswt也有的 高手请指教 想用java写个socks服务器,请教大家 Class到底是干什么用的?我看书都看不懂。。 JSplitPane 相关问题 ,请高手赐教啊 请教各位朋友,Hibernate 能不能对oracle中视图的数据进行删除。目前只能查询 Java连接数据库的问题 各位帮我看看结果应该是多少?恳请帮我详解一下,谢谢~! 奇怪的问题关于Long.parseLong() java web start 的高人请进!(有关签名的问题) java中如何设置字体的颜色? index.jsp中文无乱码,但1在index_jsp.java里调试后就有乱码了? onchange 问题
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;public class MyImage { private String fileName = null;
private Image image = null;
private Canvas canvas;
public static void main(String[] args) {
try {
MyImage window = new MyImage();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
} public void open() {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(309, 314);
shell.setText("SWT Application");
shell.open();
Button button_Scan = new Button(shell, SWT.NONE);
button_Scan.setText("浏览");
button_Scan.setBounds(3, 3, 55, 25);
button_Scan.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
FileDialog dlg =new FileDialog(shell, SWT.OPEN);
dlg.setText("Open");
dlg.setFilterNames(new String[]{"图片文件(*.jpg)","图片文件(*.gif)"});
dlg.setFilterExtensions(new String[]{"*.jpg", "*.gif"});
fileName = dlg.open();//取得打开图片的物理地址
if (fileName!=null){
image = new Image(null, fileName);
canvas.redraw();
}
}
}); canvas = new Canvas(shell, SWT.BORDER);
canvas.setBounds(0, 29, 300, 256);
canvas.addPaintListener(new PaintListener(){
public void paintControl(PaintEvent arg0) {
if (image != null) {
arg0.gc.drawImage(image, 0, 0);
}
}
});
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
swt也有的