Label的labelFor字段未按预期运行

Label的labelFor字段未按预期运行

本文介绍了Label的labelFor字段未按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Label对象的labelFor字段的用途到底是什么?

What exactly is the purpose of the labelFor field of the Label object?

昨天是我第一次听说有关JavaFX的事情,因此,如果这听起来完全是愚蠢的,我深表歉意.这个单词Label在HTML节点中对我来说似乎很熟悉.因此,我认为labelFor字段类似于label节点的for属性.不幸的是,它没有按我预期的那样工作,至少我做不到.

Yesterday was the first time I have heard about this JavaFX thing, so I apologize if this sounds entirely silly. This word Label seems familiar to me from the one in HTML nodes. For this reason, I thought that the labelFor field would be similar to the for property of a label node. Unfortunately, it did not function as I had expected, at least I couldn't make it.

在这里,让我用一个例子弄清楚:

Here, let me make it clear with an example:

Label name = new Label("Your name:");
TextField nameField = new TextField();

name.setLabelFor(nameField);

很抱歉,它并不是真正的MWE,但这是我的期望:每当用户单击标签name时,文本字段nameField都将获得焦点.

Sorry that it is not really a MWE, but here's what I was expecting with this: Whenever the user clicks over the label name, the text field nameField shall receive the focus.

据我所记得,这正是HTML中的标签/输入对发生的情况.另一方面,JavaFX中上面给出的代码没有任何反应.

This, as far as I remember, is exactly what happens with the label/input pairs in HTML. On the other hand, nothing happens with the code given above in JavaFX.

我可以通过在其他三个之后添加以下行来对其进行修补:

I can patch it to make that happen by adding the following line after the other three:

name.setOnMouseClicked(event -> name.getLabelFor().requestFocus());

但是,为什么我要首先指定labelFor?

But then, why did I specify the labelFor in the first place?

推荐答案

如javadoc中所述:

As stated in javadoc:

因此,如果您为标签设置助记符:

So if you set a mnemonic for a label:

    Label label = new Label("_Text");
    label.setMnemonicParsing(true);
    label.setLabelFor(tf);

然后使用此助记符(Alt-T)将焦点放在标记为Node的地方.

then using this mnemonic (Alt-T) will put focus into labeled Node.

labelFor还可以帮助残疾用户,该用户将获得有关从所引用标签中获取的控件的说明.

Also labelFor will help disabled user who will be provided with a description on the control taken from the referred label.

这篇关于Label的labelFor字段未按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:36