关于对awt的下拉列表框的键盘搜索操作 给choice添加KeyListener,根据输入的字符串匹配choice中的item,然后设置找到的第一个item高亮显示 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我要实现IE地址栏那种效果。加键盘事件,可以让他选中匹配的第一个item,但是不能让它展开啊,我试着在处理KeyReleased事件时给choice发送一个鼠标单击事件,想让它展开,没有反应:MouseEvent me = new MouseEvent(choice,MouseEvent.MOUSE_CLICKED,System.currentTimeMillis(),0,choice.getBounds().x,choice.getBounds().y,1,false);dispatchEvent(me);另外我试了用一个textfield,一个choice和一个popupmenu实现,但是问题又出现,我每次只能输入第一个字符。因为输入第一个字符,响应keyRelease,弹出了匹配的item的popupmenu,这时textfield就失去了焦点,...keyReleased..{String s=textField.getText();setPopupMenuItems();popupmenu.show(...);//执行到这里,textfield失去焦点并且下面的不会执行。textfield.requestFocus();//这句根本不执行,因为上面的show方法调用后直接返回了。}大家看怎么解决?我想能不能让它暂缓一段事件在响应键盘事件?这时用户可以输入多个字符了? 因为有安全问题,我把你的代码放到application里运行关于applet在客户端的安全涉及到数字签名问题,你查查FAQ获取相关信息,关于在application中的实现方法你要不要??? import java.awt.*; import java.awt.event.*; public class applet5 extends Frame { Choice ch=new Choice(); public applet5() { this.setSize(400,300); this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2); this.setResizable(false); this.setLayout(new FlowLayout()); this.add(ch); ch.addItem("aaaaaaa"); ch.addItem("bbbbbbb"); ch.addItem("ccccccc"); ch.addItem("ddddddd"); ch.addItem("eeeeeee"); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); ch.addKeyListener(new KeyAdapter(){ public void keyTyped(KeyEvent e) { try{clickChoice(e);}catch(Exception ex){System.out.println(ex.toString());} } }); this.setVisible(true); } public void clickChoice(KeyEvent e) throws Exception { String str=String.valueOf(e.getKeyChar()).toLowerCase(); String item; for(int i=0;i<ch.countItems();i++){ item=((String)ch.getItem(i)).toLowerCase(); if(item.startsWith(str)) { ch.select(i); break; } } Robot r=new Robot(); r.mouseMove(this.getX()+ch.getX()+ch.getWidth()/2,this.getY()+ch.getY()+ch.getHeight()/2); r.mousePress(InputEvent.BUTTON1_MASK); } public static void main(String args[]) { new applet5(); } } java剪切板 用java代码备份数据库,生成数据文件的问题 正则: "一行的结尾" $ 为什么不好使?? 简单的语句 求高手 怎样用java实现图片得扭曲??(急) 我的线程是不是停了?请大家帮我看看 今天面试的题目,小弟初学,哪位大哥能给个思维? java中开根号的函数是什么? 简单的人机对话,但是输出不对(源码) 急! 参与者均有分相送 . Java能否用Jni调用使用MFC的DLL? 关于 Object serialization 一段代码的问题 谢谢 怎样展开一个Choice控件
加键盘事件,可以让他选中匹配的第一个item,但是不能让它展开啊,
我试着在处理KeyReleased事件时给choice发送一个鼠标单击事件,想让它展开,没有反应:
MouseEvent me = new MouseEvent(choice,MouseEvent.MOUSE_CLICKED,System.currentTimeMillis(),0,choice.getBounds().x,choice.getBounds().y,1,false);
dispatchEvent(me);
另外我试了用一个textfield,一个choice和一个popupmenu实现,但是问题又出现,我每次只能输入第一个字符。因为输入第一个字符,响应keyRelease,弹出了匹配的item的popupmenu,这时textfield就失去了焦点,
...keyReleased..{
String s=textField.getText();
setPopupMenuItems();
popupmenu.show(...);//执行到这里,textfield失去焦点并且下面的不会执行。
textfield.requestFocus();//这句根本不执行,因为上面的show方法调用后直接返回了。
}
大家看怎么解决?我想能不能让它暂缓一段事件在响应键盘事件?这时用户可以输入多个字符了?
关于applet在客户端的安全涉及到数字签名问题,你查查FAQ获取相关信息,关于在application中的实现方法你要不要???
import java.awt.event.*; public class applet5 extends Frame {
Choice ch=new Choice();
public applet5() {
this.setSize(400,300);
this.setLocation(this.getToolkit().getScreenSize().width/2-this.getWidth()/2,this.getToolkit().getScreenSize().height/2-this.getHeight()/2);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.add(ch);
ch.addItem("aaaaaaa");
ch.addItem("bbbbbbb");
ch.addItem("ccccccc");
ch.addItem("ddddddd");
ch.addItem("eeeeeee");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
ch.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e) {
try{clickChoice(e);}catch(Exception ex){System.out.println(ex.toString());}
}
});
this.setVisible(true);
} public void clickChoice(KeyEvent e) throws Exception {
String str=String.valueOf(e.getKeyChar()).toLowerCase();
String item;
for(int i=0;i<ch.countItems();i++){
item=((String)ch.getItem(i)).toLowerCase();
if(item.startsWith(str)) {
ch.select(i);
break;
}
}
Robot r=new Robot();
r.mouseMove(this.getX()+ch.getX()+ch.getWidth()/2,this.getY()+ch.getY()+ch.getHeight()/2);
r.mousePress(InputEvent.BUTTON1_MASK);
} public static void main(String args[]) {
new applet5();
}
}