Java的奇怪想象

hjxaut 2008-03-02 08:17:46
我的程序如下:
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依次下移,但是当移到最后一项时,则重新选择第一项。但上述程序当移动到最后一项,却选择第二项而不是第一项,这是为什么呢??
...全文
45 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
老紫竹 2008-03-02
  • 打赏
  • 举报
回复
总算实现了,增加了一个记录上一个位置的变量,请参考
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 | 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;
}
});
//
}
}
老紫竹 2008-03-02
  • 打赏
  • 举报
回复
Sorry!虽然现象估计准确,但不知道怎么屏蔽默认的移动。楼上的代码无效!!
老紫竹 2008-03-02
  • 打赏
  • 举报
回复
我想有一个问题,你每次按向下的时候,我并没有看到你移动index的代码
也就是说,这个组件会自动向下移动。
如果这样的话,你应该选中-1的位置,这样组件自动下移的时候,就会变成选中了0

list.select(-1);

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧