gpt4 book ai didi

javafx - 选项卡式 Pane 快捷方式

转载 作者:行者123 更新时间:2023-12-03 20:23:01 25 4
gpt4 key购买 nike

我正在尝试创建一个笔记本(在 JavaFX 中),其中选项卡的快捷方式与按钮非常相似。因此,在以下示例中,有一个带有“Labor”、“Parts”和“Tax”的笔记本。
enter image description here
用户当然可以单击选项卡,但他们也可以单击 Alt+L、Alt+P、Alt+T 分别选择人工、零件和税收 Pane 。
查看文档,似乎标准快捷方式行为来自 Labeled 类,Tab 不属于该类。我如何将此行为放入我的选项卡式 Pane 选项卡中?
编辑:更具体地说,我正在寻找与现有行为和控制代码相匹配的视觉效果,它不需要控制代码知道热键是什么。
因此,与按钮一样,用户按下元键(CTRL、ALT、OPT 等等),相应的字符会在自身下划线,当他们按下该键时,父级可能必须搜索选项卡的文本才能知道是哪个一个选择。

最佳答案

您只需要注册您自己的EventFilterScene .这将使您能够聆听所需的键盘组合并做出相应的 react 。
在下面的示例应用程序中,您将传递所需的 TabKeyCombination到处理 EventFilter 注册的方法.
在这个例子中,我配置了 CTRL+1 的快捷方式, CTRL+2 , 和 CTRL+3选择 Tab分别为 1、2 或 3。
请注意,您也可以使用 KeyCombination.CONTROL_DOWN而不是 KeyCombination.SHORTCUT_DOWN ,但使用 SHORTCUT_DOWN是首选,因为它是独立于平台的。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TabPaneShortcuts extends Application {

public static void main(String[] args) {

launch(args);
}

@Override
public void start(Stage primaryStage) {

// **********************************************************************************************
// Create a basic layout
// **********************************************************************************************
VBox root = new VBox(5);
root.setAlignment(Pos.TOP_CENTER);
root.setPadding(new Insets(10));

// **********************************************************************************************
// Create a TabPane
// **********************************************************************************************
TabPane tabPane = new TabPane();

// **********************************************************************************************
// Create the Tabs
// **********************************************************************************************
Tab tab1 = new Tab("Labor");
Tab tab2 = new Tab("Parts");
Tab tab3 = new Tab("Tax");
tabPane.getTabs().addAll(tab1, tab2, tab3);

// **********************************************************************************************
// Add the TabPane to our root layout
// **********************************************************************************************
root.getChildren().add(tabPane);

// **********************************************************************************************
// Set the Scene for the stage
// **********************************************************************************************
Scene scene = new Scene(root);
primaryStage.setScene(scene);

// **********************************************************************************************
// Register keyboard shortcuts to select Tabs with the keyboard. Here, we create the KeyCodeCombination
// to listen for CTRL + 1,2, or 3.
// **********************************************************************************************
registerShortcut(tabPane, tab1, scene,
new KeyCodeCombination(KeyCode.DIGIT1,
KeyCombination.SHORTCUT_DOWN));
registerShortcut(tabPane, tab2, scene,
new KeyCodeCombination(KeyCode.DIGIT2,
KeyCombination.SHORTCUT_DOWN));
registerShortcut(tabPane, tab3, scene,
new KeyCodeCombination(KeyCode.DIGIT3,
KeyCombination.SHORTCUT_DOWN));

// **********************************************************************************************
// Configure the Stage
// **********************************************************************************************
primaryStage.setWidth(300);
primaryStage.setHeight(200);
primaryStage.setTitle("Test Application");
primaryStage.show();
}

/**
* Registers the given KeyCombination to automatically select the given Tab of the TabPane
*
* @param tabPane The TabPane whose selection model we need to manipulate
* @param tab The Tab to be selected when the given KeyCombination is detected
* @param scene The main Scene on which to register this EventFilter
* @param combination The KeyCombination to listen for
*/
private void registerShortcut(TabPane tabPane, Tab tab, Scene scene, KeyCombination combination) {

// **********************************************************************************************
// Add an EventFilter to the Scene to listen for the given KeyCombination
// **********************************************************************************************
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
// **********************************************************************************************
// If the event matches the KeyCombination passed to this method, select the desired Tab
// **********************************************************************************************
if (combination.match(event)) {
tabPane.getSelectionModel().select(tab);
}
});

}
}

您的问题特别提到使用 ALT+[letter]对于组合,这也可以轻松完成:
registerShortcut(tabPane, tab1, scene,
new KeyCodeCombination(KeyCode.L,
KeyCombination.ALT_DOWN));
我选择了 CTRL仅仅因为使用 ALT导致我的 Windows 10 系统每次都“叮”一声,我懒得弄清楚原因。 :)

关于javafx - 选项卡式 Pane 快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67450859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com