虽然@ItachiUchiha解决方案有效,但正如他所说,这取决于布局(box
在他的样本中).
基于此问题,您可以修改a的默认行为TextArea
,而不管布局如何.
但是您需要使用此私有API,它可能随时更改,恕不另行通知.
在此示例中Tab
,并Shitf+Tab
具有所需的行为,而Ctrl+Tab
将插入 "\t"
的文本区域.
@Override
public void start(Stage primaryStage) {
TextArea area = new TextArea();
area.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent event) -> {
if (event.getCode() == KeyCode.TAB) {
TextAreaSkin skin = (TextAreaSkin) area.getSkin();
if (skin.getBehavior() instanceof TextAreaBehavior) {
TextAreaBehavior behavior = (TextAreaBehavior) skin.getBehavior();
if (event.isControlDown()) {
behavior.callAction("InsertTab");
} else if (event.isShiftDown()) {
behavior.callAction("TraversePrevious");
} else {
behavior.callAction("TraverseNext");
}
event.consume();
}
}
});
VBox root = new VBox(20, new Button("Button 1"), area, new Button("Button 2"));
Scene scene = new Scene(root, 400, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1424 次 |
最近记录: |