gpt4 book ai didi

java - Nimbus LAF 的 Mac 键盘快捷键

转载 作者:搜寻专家 更新时间:2023-10-31 20:14:51 25 4
gpt4 key购买 nike

有没有办法在 OS X 上使用 Nimbus LAF(外观),同时仍然能够使用 Meta 键进行剪切/复制/粘贴和全选操作?

我目前在我的 Swing 应用程序的主要方法中有以下代码,它根据操作系统更改 LAF(OS X 的默认设置,所有其他系统的 Nimbus):

if (!System.getProperty("os.name", "").startsWith("Mac OS X")) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(LicenseInspectorUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
}

我这样做是为了解决这个问题,因为 Nimbus 覆盖了 OS X 上的剪切/复制/粘贴和全选键盘快捷键(Meta 键与 Ctrl 键)。如果键盘快捷键没有被覆盖,我宁愿一直使用 Nimbus。

最佳答案

使用 getMenuShortcutKeyMask()方法与 NimbusLookAndFeel 一起使用以启用 键,如 example 所示.另见此相关 answer .

JTextField 的特殊情况下,您可以在 key binding 中使用掩码唤起原始 Action 。

int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
JTextField jtf = new JTextField("Test");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, MASK), "select-all");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, MASK), "copy");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_X, MASK), "cut");
jtf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_V, MASK), "paste");

关于java - Nimbus LAF 的 Mac 键盘快捷键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780028/

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