这段程序是 创建一个名为“点击我”的button,当你按下这个button时回出现一个list列表,点击list里面的内容,会在eclipse控制台输出相应的内容。
这个程序有个小问题,就是点击button后在界面的下方出现一个list,而不是在button下面出现一个list,我想要在button下面显示一个list,但是不知道怎么做,还请高手指点一下 谢谢啦^_^。
开发环境 elipse+swtdesigner
代码:
import
org.eclipse.swt.SWT;
import org.eclipse.swt.custom.PopupList;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class PopuplistTT {
protected Shell shell;
private static final String [] PUSH = {"0","1","2","3","4","5"};
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
PopuplistTT window = new PopuplistTT();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final 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(500, 375);
shell.setText("SWT Application");
final Button button = new Button(shell, SWT.NONE);
button.setText("点击我");
button.setBounds(0, 0, 100, 23);
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){
PopupList list = new PopupList(shell);
list.setItems(PUSH);
String selected = list.open(shell.getBounds());
System.out.println(selected);
}
});
}
}
import org.eclipse.swt.custom.PopupList;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class PopuplistTT {
protected Shell shell;
private static final String [] PUSH = {"0","1","2","3","4","5"};
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
PopuplistTT window = new PopuplistTT();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window
*/
public void open() {
final 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(500, 375);
shell.setText("SWT Application");
final Button button = new Button(shell, SWT.NONE);
button.setText("点击我");
button.setBounds(0, 0, 100, 23);
button.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent event){
PopupList list = new PopupList(shell);
list.setItems(PUSH);
String selected = list.open(shell.getBounds());
System.out.println(selected);
}
});
}
}