设置助记符
助记符是数字或字符,在用户界面组件的菜单标题(按钮,文本字段等)中,通常带有下划线。如果您将此字符与Alt键一起按下,则相应菜单项将被聚焦。
创建一个助记符-
通过实例化其各自的类来创建任何节点。
创建一个标签以将节点与所需助记符之前的下划线字符(“ _”)相关联。
默认情况下,标签的助记符解析值将为false,使用setMnemonicParsing()方法将此值设置为true。
将标签设置/关联到所需的节点。
将标签和字段添加到场景。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class LabelForExample extends Application {
public void start(Stage stage) {
//Creating nodes
TextField textField = new TextField();
PasswordField pwdField = new PasswordField();
//creating labels
Label label1 = new Label("_Email");
label1.setMnemonicParsing(true);
label1.setLabelFor(textField);
Label label2 = new Label("_Password");
label2.setMnemonicParsing(true);
label2.setLabelFor(pwdField);
//Adding labels for nodes
HBox box1 = new HBox(5);
box1.setPadding(new Insets(25, 5 , 5, 50));
box1.getChildren().addAll(label1, textField, label2, pwdField);
//Setting the stage
Scene scene = new Scene(box1, 595, 150, Color.BEIGE);
stage.setTitle("Check Box Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果
如果按键盘上的Alt + e,则焦点将移至第一个文本字段;如果按键盘上的Alt + p,则焦点将移至第二个文本字段