你格式太乱,我看得眼花。帮你该了一下,你自己在调整一下。问题原因见代码注视,还有如果是自己手工做 GUI 的话建议使用 JFace 包即使用工具生成 GUI 代码,你起码也要手工做几个小程序试试======================================================================
/*
* Created on 2005-4-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;public class Test {
private final Image img1 = new Image(null, "image/1.jpg");
private final Image img2 = new Image(null, "image/2.jpg"); private org.eclipse.swt.widgets.Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"
private Button button = null;
private Composite composite = null;
private int imgCounter = 0;
/**
* This method initializes composite
*
*/
private void createComposite() {
composite = new Composite(sShell, SWT.NONE);
composite.setBounds(new org.eclipse.swt.graphics.Rectangle(128,12,260,183));
composite.setLayout(new GridLayout());
composite.setLayoutData(new GridData());
}
public static void main(String[] args) {
/* Before this is run, be sure to set up the following in the launch configuration
* (Arguments->VM Arguments) for the correct SWT library path.
* The following is a windows example:
* -Djava.library.path="installation_directory\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86"
*/
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display.getDefault();
Test thisClass = new Test();
thisClass.createSShell() ;
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep ();
}
display.dispose();
} /**
* This method initializes sShell
*/
private void createSShell() {
sShell = new org.eclipse.swt.widgets.Shell();
//sShell.setLayout(new GridLayout());
button = new Button(sShell, SWT.NONE);
createComposite();
sShell.setText("Shell");
button.setBounds(new org.eclipse.swt.graphics.Rectangle(9,2,79,23));
button.setText("Test");
sShell.setSize(new org.eclipse.swt.graphics.Point(411,235));
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
System.out.println("widgetSelected()");
composite.redraw(); //重绘
}
});
/**SWT 的绘图代码要添在 PaintListener 里面*/
composite.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent arg0) {
GC gc = arg0.gc;
if (imgCounter %2 == 0) {
gc.drawImage(img1, 0 , 0);
//new Button(composite, SWT.RADIO);
System.out.println("hello, i am img1");
} else {
//new Button(composite, SWT.ARROW_DOWN);
gc.drawImage(img2, 0, 0);
System.out.println("hello, i am img2");
}
imgCounter ++;
composite.layout(true);
composite.getShell().layout(true);
}
});
}
}
/*
* Created on 2005-4-4
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;public class Test {
private final Image img1 = new Image(null, "image/1.jpg");
private final Image img2 = new Image(null, "image/2.jpg"); private org.eclipse.swt.widgets.Shell sShell = null; // @jve:decl-index=0:visual-constraint="10,10"
private Button button = null;
private Composite composite = null;
private int imgCounter = 0;
/**
* This method initializes composite
*
*/
private void createComposite() {
composite = new Composite(sShell, SWT.NONE);
composite.setBounds(new org.eclipse.swt.graphics.Rectangle(128,12,260,183));
composite.setLayout(new GridLayout());
composite.setLayoutData(new GridData());
}
public static void main(String[] args) {
/* Before this is run, be sure to set up the following in the launch configuration
* (Arguments->VM Arguments) for the correct SWT library path.
* The following is a windows example:
* -Djava.library.path="installation_directory\plugins\org.eclipse.swt.win32_3.0.0\os\win32\x86"
*/
org.eclipse.swt.widgets.Display display = org.eclipse.swt.widgets.Display.getDefault();
Test thisClass = new Test();
thisClass.createSShell() ;
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep ();
}
display.dispose();
} /**
* This method initializes sShell
*/
private void createSShell() {
sShell = new org.eclipse.swt.widgets.Shell();
//sShell.setLayout(new GridLayout());
button = new Button(sShell, SWT.NONE);
createComposite();
sShell.setText("Shell");
button.setBounds(new org.eclipse.swt.graphics.Rectangle(9,2,79,23));
button.setText("Test");
sShell.setSize(new org.eclipse.swt.graphics.Point(411,235));
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
System.out.println("widgetSelected()");
composite.redraw(); //重绘
}
});
/**SWT 的绘图代码要添在 PaintListener 里面*/
composite.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent arg0) {
GC gc = arg0.gc;
if (imgCounter %2 == 0) {
gc.drawImage(img1, 0 , 0);
//new Button(composite, SWT.RADIO);
System.out.println("hello, i am img1");
} else {
//new Button(composite, SWT.ARROW_DOWN);
gc.drawImage(img2, 0, 0);
System.out.println("hello, i am img2");
}
imgCounter ++;
composite.layout(true);
composite.getShell().layout(true);
}
});
}
}
用的就是楼主的程序。
开始我的SWT之旅!可惜没有办法搞图,否则把我的过程写出来和菜鸟门一起分享。
我是摸了一二个钟才搞定它!我感觉好象SWT的原码程序很少啊。在网上一搜索,全是swing和awt的。
我之前搞到一个swing的扫雷,也编译过了,但是有点问题。