5

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

我目前在我的 Swing 应用程序的 main 方法中有以下代码,它根据操作系统更改 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。

4

2 回答 2

3

使用该getMenuShortcutKeyMask()方法可NimbusLookAndFeel启用⌘</kbd> key, as shown in this example. See also this related answer.

在 a 的特定情况下,您可以在调用原始操作的键绑定JTextField中使用掩码。

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");
于 2012-03-20T05:00:14.577 回答
1

不同的组件使用不同的键,因此要映射所有组件,您必须定义不同的键。例如(从这里找到的基础):

private void addOSXKeyStrokes(InputMap inputMap) {
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), DefaultEditorKit.copyAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_X, KeyEvent.META_DOWN_MASK), DefaultEditorKit.cutAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_DOWN_MASK), DefaultEditorKit.pasteAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_DOWN_MASK), DefaultEditorKit.selectAllAction);
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), "copy");
  inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.META_DOWN_MASK), "selectAll");
}

然后可以将其映射到不同的组件,如下所示:

// This must be performed immediately after the LaF has been set
if (System.getProperty("os.name", "").startsWith("Mac OS X")) {
  // Ensure OSX key bindings are used for copy, paste etc
  // Use the Nimbus keys and ensure this occurs before any component creation
  addOSXKeyStrokes((InputMap) UIManager.get("EditorPane.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("FormattedTextField.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("PasswordField.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("TextField.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("TextPane.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("TextArea.focusInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("Table.ancestorInputMap"));
  addOSXKeyStrokes((InputMap) UIManager.get("Tree.focusInputMap"));
}

Aqua(OS X Look and Feel)动作名称的完整列表在这里

于 2017-08-22T08:04:44.923 回答