📄 editkeybinding.java
字号:
gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 9; gridBagConstraints.gridwidth = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(2, 6, 2, 6); getContentPane().add(jPanel1, gridBagConstraints); pack(); }//GEN-END:initComponents private void initDialog() { String description = menuItem.getDescription(); header.setText("Add Key Binding for: "+description); pack(); } // ------------------------------------- Actions ------------------------------------ /** Ignore input key typed events */ private void stroke2InputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stroke2InputKeyTyped evt.consume(); }//GEN-LAST:event_stroke2InputKeyTyped /** Ignore input key released events */ private void stroke2InputKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stroke2InputKeyReleased evt.consume(); }//GEN-LAST:event_stroke2InputKeyReleased /** * Respond to key input to text box. Show string in the text box * representing that KeyStroke. * @param evt the KeyEvent */ private void stroke2InputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stroke2InputKeyPressed if (!KeyBindingManager.validKeyEvent(evt)) return; key2 = KeyStroke.getKeyStrokeForEvent(evt); stroke2Input.setText(KeyStrokePair.keyStrokeToString(key2)); updateConflicts(); evt.consume(); }//GEN-LAST:event_stroke2InputKeyPressed /** Ignore input key typed events */ private void stroke1InputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stroke1InputKeyTyped evt.consume(); }//GEN-LAST:event_stroke1InputKeyTyped /** Ignore input key released events */ private void stroke1InputKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stroke1InputKeyReleased evt.consume(); }//GEN-LAST:event_stroke1InputKeyReleased /** * Respond to key input to text box. Show string in the text box * representing that KeyStroke. * @param evt the KeyEvent */ private void stroke1InputKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_stroke1InputKeyPressed if (!KeyBindingManager.validKeyEvent(evt)) return; key1 = KeyStroke.getKeyStrokeForEvent(evt); stroke1Input.setText(KeyStrokePair.keyStrokeToString(key1)); updateConflicts(); evt.consume(); }//GEN-LAST:event_stroke1InputKeyPressed private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed setVisible(false); dispose(); }//GEN-LAST:event_cancelButtonActionPerformed private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed KeyStroke prefixStroke = getStroke(stroke1Input); KeyStroke stroke = getStroke(stroke2Input); if (prefixStroke == null) { dialogBadInput(); return; } ListModel model = conflictsList.getModel(); // if conflicts exist, ask user if they want to remove or leave them (or do nothing) int removeConflicts = 1; if (model.getSize() != 0) { removeConflicts = removeConflictsDialog(model); // removeConflicts: 0 to remove all, 1 to leave all conflicts, 2 to cancel (default) } if (removeConflicts == 2) { return; // do nothing } else if (removeConflicts == 0) { List<KeyBindings> conflicts = getConflicts(); for (KeyBindings k : conflicts) { // remove all bindings in k for (Iterator<KeyStrokePair> kit = k.getKeyStrokePairs(); kit.hasNext(); ) { KeyStrokePair pair = kit.next(); menuBar.removeKeyBinding(k.getActionDesc(), pair); } } } // add new key binding if (stroke == null) { stroke = prefixStroke; prefixStroke = null; } menuBar.addUserKeyBinding(menuItem, stroke, prefixStroke); setVisible(false); }//GEN-LAST:event_addButtonActionPerformed private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog }//GEN-LAST:event_closeDialog // --------------------------------- List Box ---------------------------------- private void updateConflicts() { List<KeyBindings> conflicts = getConflicts(); if (conflicts == null || conflicts.size() == 0) { clearConflictsList(); } else { // create array of strings to display in list box String [] objects = new String[conflicts.size()]; for (int i=0; i<conflicts.size(); i++) { KeyBindings k = conflicts.get(i); String s = k.getActionDesc() + " [ "+k.bindingsToString()+" ]"; objects[i] = s; } // update list box conflictsList.setListData(objects); return; } // warn if an ALT key conflicts with a pulldown menu if ((key1.getModifiers() & InputEvent.ALT_MASK) != 0) { TopLevel top = TopLevel.getCurrentJFrame(); EMenuBar menuBar = top.getEMenuBar(); for (EMenuItem menu: menuBar.getItems()) { char reservedAlt = menu.getMnemonicChar(); if (reservedAlt == 0) continue; KeyStroke ksRes = KeyStroke.getKeyStroke(reservedAlt, InputEvent.ALT_MASK); if (ksRes == key1) { String [] objects = new String[1]; objects[0] = "Pulldown menu: " + menu.getText(); conflictsList.setListData(objects); return; } } }// // check for reserved keystrokes (doesn't work)// KeyStroke key1 = getStroke(stroke1Input);// Keymap map = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP);// for ( ; map != null; map = map.getResolveParent())// {// KeyStroke[] keys = map.getBoundKeyStrokes();// for (int i=0; i<keys.length; i++)// {// if (keys[i] == key1)// {// System.out.println(key1 + " IS RESERVED");// }// }// } } /** * Clear conflicts list */ private void clearConflictsList() { conflictsList.setListData(new Object [] {}); } /** * Get list of KeyBindings that conflict with key combo user * has entered into this dialog. * @return a list of KeyBindings, or null if none. */ private List<KeyBindings> getConflicts() { // grab key combo from input boxes KeyStroke key1 = getStroke(stroke1Input); if (key1 == null) { clearConflictsList(); return null; } KeyStroke key2 = getStroke(stroke2Input); // if key2 is null, then key1 is the primary stroke. // otherwise, key1 is the prefix stroke, and key2 is the primary stroke KeyStroke prefixStroke = key1, stroke = key2; // assumes key2 non-null if (key2 == null) { stroke = key1; prefixStroke = null; } KeyStrokePair pair = KeyStrokePair.getKeyStrokePair(prefixStroke, stroke); return KeyBindingManager.getConflictsAllManagers(pair); } /** * Get KeyStroke from input field. * @param field a JTextField with text that describes a KeyStroke * @return the KeyStroke, or null if invalid text in field. */ private KeyStroke getStroke(JTextField field) { String str = field.getText(); return KeyStrokePair.stringToKeyStroke(str); } // ------------------------------ Helper Dialogs ------------------------------ /** * Pops up a JOptionPain dialog box to ask user if they want to remove or leave * conflicting bindings, or just cancel. * @return 0 to remove all, 1 to leave all conflicts, 2 to cancel (default) */ private int removeConflictsDialog(ListModel model) { StringBuffer buf = new StringBuffer(); buf.append("The following conflicts exists. Would you like to remove them?\n"); for (int i=0; i<model.getSize(); i++) { buf.append(model.getElementAt(i).toString()+"\n"); } Object [] options = {"Remove All", "Leave All", "Cancel"}; int n = JOptionPane.showOptionDialog(this, buf.toString(), "Conflicts Exist", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[2]); return n; } private void dialogBadInput() { JOptionPane.showMessageDialog(null, "I cannot bind to the First Stroke key you have specified. Sorry", "Invalid Input", JOptionPane.WARNING_MESSAGE); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton addButton; private javax.swing.JButton cancelButton; private javax.swing.JLabel conflictsLabel; private javax.swing.JList conflictsList; private javax.swing.JLabel header; private javax.swing.JPanel jPanel1; private javax.swing.JSeparator jSeparator1; private javax.swing.JSeparator jSeparator2; private javax.swing.JSeparator jSeparator3; private javax.swing.JLabel optionalLabel; private javax.swing.JTextField stroke1Input; private javax.swing.JLabel stroke1Label; private javax.swing.JTextField stroke2Input; private javax.swing.JLabel stroke2Label; // End of variables declaration//GEN-END:variables }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -