JavaFXFocusHandler?
我刚刚从AWT改为JavaFX,我想知道如何使用focus。 例如:在AWT中,我写了这样的东西:JavaFXFocusHandler?,javafx,focuslistener,Javafx,Focuslistener,我刚刚从AWT改为JavaFX,我想知道如何使用focus。 例如:在AWT中,我写了这样的东西: Button bFocus = new Button("Focus"); bFocus.addFocusListener(new FocusListener() { public void focusGained(FocusEvent e) { System.out.println("Having the Focus"); }
Button bFocus = new Button("Focus");
bFocus.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
System.out.println("Having the Focus");
}
public void focusLost(FocusEvent e) {
System.out.println("Lost the Focus");
}
});
但是它在JavaFX中是如何工作的呢?我尝试了许多不同的方法,但都不起作用……JavaFX有一个API,定义了可以注册监听器并在监听器发生变化时做出响应的API。在JavaFX中,几乎所有属于UI元素的状态都由这些属性表示,允许您注册一个监听器,当它们发生变化时进行响应 因此,例如,所有UI元素的超类都有一个名为ReadOnlyBooleanProperty,您可以使用它注册一个侦听器:
Button bFocus = new Button("Focus");
bFocus.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
if (isNowFocused) {
System.out.println("Having the Focus");
} else {
System.out.println("Lost the Focus");
}
});
我认为看到一个将ChangeListener指定为匿名内部类的示例可能会有所帮助,就像这里提到的James_D一样
TextField yourTextField = new TextField();
yourTextField.focusedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> arg0, Boolean oldPropertyValue, Boolean newPropertyValue)
{
if (newPropertyValue)
{
System.out.println("Textfield on focus");
}
else
{
System.out.println("Textfield out focus");
}
}
});
TextField yourTextField=newtextfield();
yourTextField.focusedProperty().addListener(新的ChangeListener())
{
@凌驾
公共无效更改(为什么重复一个过时的答案?如果你看到任何优势,请描述它们:)