JavaFX Label setLabelFor() 方法示例
在 JavaFX 中,你可以通过实例化javafx.scene.control.Label类来创建标签。此类提供一个名为labelFor()的方法。使用此方法,你可以将当前标签设置成另一个控制节点的标签。
在设定助记符和解析加速器时,此方法会很方便。
示例
在以下 JavaFX 示例中,我们创建了一个标签(文字)、一个文本字段和一个按钮。使用labelFor()方法,我们将标签(文字)与文本字段关联起来,从而启用助记符解析(T)。因此,在输出窗口中,如果你按Alt + T,此文本字段将获得焦点。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class LabelFor_Example extends Application {
public void start(Stage stage) {
//Creating nodes
TextField textField = new TextField();
Button button = new Button("Click Me");
//creating labels
Label label1 = new Label("_Text");
label1.setMnemonicParsing(true);
label1.setLabelFor(textField);
//Adding labels for nodes
HBox box1 = new HBox(5);
box1.setPadding(new Insets(25, 5 , 5, 50));
box1.getChildren().addAll(label1, textField, button);
//Setting the stage
Scene scene = new Scene(box1, 595, 150, Color.BEIGE);
stage.setTitle("JavaFX Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出

广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP