我的程序如下:
package hj.com; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell; public class testList { protected Shell shell; /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
testList window = new testList();
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 List list=new List(shell,SWT.BORDER);
// list.select(0);
list.setItems(new String[]{"第0","第1","第2","第3","第4"});
list.setBounds(0,0,50,200);
list.setSelection(0); list.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int curIndex=list.getSelectionIndex();
int total=list.getItemCount();
if(e.keyCode==SWT.ARROW_UP){
if(curIndex==0){
list.select(total-1);
int c=list.getSelectionIndex();
System.out.println(c);
}
}else if(e.keyCode==SWT.ARROW_DOWN){
if(curIndex==total-1){
list.select(0);
int c=list.getSelectionIndex();
System.out.println(c);
}
}
} }); //
} }
我的功能是当按光标向下时,LISH中被选中的item依次下移,但是当移到最后一项时,则重新选择第一项。但上述程序当移动到最后一项,却选择第二项而不是第一项,这是为什么呢??
package hj.com; import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell; public class testList { protected Shell shell; /**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
testList window = new testList();
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 List list=new List(shell,SWT.BORDER);
// list.select(0);
list.setItems(new String[]{"第0","第1","第2","第3","第4"});
list.setBounds(0,0,50,200);
list.setSelection(0); list.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
int curIndex=list.getSelectionIndex();
int total=list.getItemCount();
if(e.keyCode==SWT.ARROW_UP){
if(curIndex==0){
list.select(total-1);
int c=list.getSelectionIndex();
System.out.println(c);
}
}else if(e.keyCode==SWT.ARROW_DOWN){
if(curIndex==total-1){
list.select(0);
int c=list.getSelectionIndex();
System.out.println(c);
}
}
} }); //
} }
我的功能是当按光标向下时,LISH中被选中的item依次下移,但是当移到最后一项时,则重新选择第一项。但上述程序当移动到最后一项,却选择第二项而不是第一项,这是为什么呢??
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell; public class TestList {
protected Shell shell; /**
* Launch the application
*
* @param args
*/
public static void main(String[] args) {
try {
TestList window = new TestList();
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 List list = new List(shell, SWT.BORDER | SWT.V_SCROLL);
// list.select(0);
list.setItems(new String[] { "第0", "第1", "第2", "第3", "第4" });
list.setBounds(0, 0, 50, 200);
list.setSelection(0);
list.addKeyListener(new KeyAdapter() {
int lastIndex = -1; public void keyReleased(KeyEvent e) {
int curIndex = list.getSelectionIndex();
System.out.println("c=" + curIndex);
int total = list.getItemCount();
if (e.keyCode == SWT.ARROW_UP) {
if (lastIndex == 0) {
list.select(total - 1);
curIndex = total-1;
list.showSelection();
int c = list.getSelectionIndex();
System.out.println(c);
}
} else if (e.keyCode == SWT.ARROW_DOWN) {
if (lastIndex == total - 1) {
list.select(0);
curIndex = 0;
list.showSelection();
int c = list.getSelectionIndex();
System.out.println(c);
}
}
lastIndex = curIndex;
}
});
//
}
}