JavaFX文本框、按钮、列表框事件监听处理
javafx文本框、按钮、列表框事件监听处理
textfield textfield = new textfield();
//监听文本框输入值变化
textfield.textproperty().addlistener(this::textchange);
private void textchange(observablevalueobservable, string oldvalue, string newvalue){
}
//监听文本框焦点变化
textfield.focusedproperty().addlistener(this::focusedchange);
private void focusedchange(observablevalueobservable, boolean oldvalue, boolean newvalue){
}
//按键点击
textfield.setonkeypressed(new eventhandler() {
@override
public void handle(keyevent event) {
if(event.getcode().equals(keycode.enter)){
}
if(event.getcode().equals(keycode.up) && (selectedindex - 1) < stationnum){
//event.consume();可阻止原生事件进行,即文本框光标移动
}
if(event.getcode().equals(keycode.down) && (selectedindex + 1) > -1){
}
}
});
//按钮
button btn = new button("确定");
//点击
btn.setonaction(new eventhandler() {
@override
public void handle(actionevent event) {
}
});
//列表框
listview listview = new listview();
listview.addeventhandler(mouseevent.mouse_clicked, e ->{
//双击
if (e.getclickcount() == 2) {
}
//单击
if (e.getclickcount() == 1) {
}
});
//按键
listview.addeventhandler(keyevent.key_pressed, e ->{
//回车
if (e.getcode() == keycode.enter) {
}
//上键
if(e.getcode().equals(keycode.up)){
}
//下键
if(e.getcode().equals(keycode.down)){
}
});