javax.swing.JButton.getActionMap()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(219)

本文整理了Java中javax.swing.JButton.getActionMap()方法的一些代码示例,展示了JButton.getActionMap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JButton.getActionMap()方法的具体详情如下:
包路径:javax.swing.JButton
类名称:JButton
方法名:getActionMap

JButton.getActionMap介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

  1. // create an Action doing what you want
  2. Action action = new AbstractAction("doSomething") {
  3. @Override
  4. public void actionPerformed(ActionEvent e) {
  5. System.out.println("triggered the action");
  6. }
  7. };
  8. // configure the Action with the accelerator (aka: short cut)
  9. action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));
  10. // create a button, configured with the Action
  11. JButton toolBarButton = new JButton(action);
  12. // manually register the accelerator in the button's component input map
  13. toolBarButton.getActionMap().put("myAction", action);
  14. toolBarButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
  15. (KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "myAction");

代码示例来源:origin: stackoverflow.com

  1. JToolBar bar = new JToolBar();
  2. JButton toolBarButton = bar.add(action);
  3. toolBarButton.getActionMap().put("myAction", action);
  4. toolBarButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
  5. (KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "myAction");

代码示例来源:origin: stackoverflow.com

  1. JButton clear = new JButton("Clear");
  2. InputMap im = clear .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
  3. ActionMap am = clear .getActionMap();
  4. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "clear");
  5. am.put("clear", new ClearFieldsAction());

代码示例来源:origin: google/sagetv

  1. public static void fixCancelButton(final javax.swing.JButton cancelButton)
  2. {
  3. cancelButton.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).
  4. put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0),
  5. "Cancel");
  6. cancelButton.getActionMap().put("Cancel",
  7. new javax.swing.AbstractAction()
  8. {
  9. public void actionPerformed(java.awt.event.ActionEvent evt)
  10. {
  11. cancelButton.doClick();
  12. }
  13. });
  14. }

代码示例来源:origin: google/sagetv

  1. public static void fixOKButton(final javax.swing.JButton okButton)
  2. {
  3. okButton.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).
  4. put(javax.swing.KeyStroke.getKeyStroke(
  5. java.awt.event.KeyEvent.VK_ENTER, 0), "OK");
  6. okButton.getActionMap().put("OK",
  7. new javax.swing.AbstractAction()
  8. {
  9. public void actionPerformed(java.awt.event.ActionEvent evt)
  10. {
  11. okButton.doClick();
  12. }
  13. });
  14. }

代码示例来源:origin: stackoverflow.com

  1. JButton button = new JButton(nextStepAction);
  2. button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "enter");
  3. button.getActionMap().put("enter", nextStepAction);

代码示例来源:origin: stackoverflow.com

  1. NumberAction action = new NumberAction(1);
  2. JButton btn = new JButton(action);
  3. InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
  4. ActionMap am = btn.getActionMap();
  5. im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "Number1");
  6. am.put("Number1", action);

代码示例来源:origin: org.jclarion/clarion-runtime

  1. @Override
  2. public void actionPerformed(ActionEvent e) {
  3. JButton b= button;
  4. if (b!=null && isEnabled()) {
  5. button.getActionMap().get(type).actionPerformed(e);
  6. }
  7. }

代码示例来源:origin: stackoverflow.com

  1. JButton btn = new JButton("Open Window"); //creates the button
  2. //registers the KeyStroke with the button
  3. btn.getInputMap().put(KeyStroke.getKeyStroke("released ENTER"), "released");
  4. //creates an action for the component
  5. Action exit = new AbstractAction() {
  6. public void actionPerformed(ActionEvent e) {
  7. JOptionPane.showMessageDialog(btn, "You pressed the enter key");
  8. }
  9. };
  10. //registers the Action with the corresponding keystroke on the button component
  11. //@param exit is the action to be performed
  12. btn.getActionMap().put("released",exit);

代码示例来源:origin: stackoverflow.com

  1. KeyStroke keySave = KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK);
  2. Action performSave = new AbstractAction("Save") {
  3. public void actionPerformed(ActionEvent e) {
  4. //do your save
  5. System.out.println("save");
  6. }
  7. };
  8. JButton b1 = new JButton(performSave);
  9. b1.getActionMap().put("performSave", performSave);
  10. b1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keySave, "performSave");
  11. KeyStroke keyExit = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK);
  12. Action performExit = new AbstractAction("Exit") {
  13. public void actionPerformed(ActionEvent e) {
  14. //exit
  15. System.out.println("exit");
  16. }
  17. };
  18. JButton b2 = new JButton(performExit);
  19. b2.getActionMap().put("performExit", performExit);
  20. b2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyExit, "performExit");

代码示例来源:origin: stackoverflow.com

  1. private void createButton( int x, int y, int h, int w, String actionCommand, String name, int... keys ) {
  2. nAction na = new nAction( actionCommand );
  3. JButton btn = new JButton( na );
  4. btn.setName( name );
  5. InputMap im = btn.getInputMap( WHEN_IN_FOCUSED_WINDOW );
  6. ActionMap am = btn.getActionMap();
  7. for ( int virtualKey : keys ) {
  8. im.put( KeyStroke.getKeyStroke( virtualKey, 0, false ), "number-pressed" );
  9. im.put( KeyStroke.getKeyStroke( virtualKey, 0, true ), "number-released" );
  10. }
  11. am.put( "number-pressed", new NumberKeyPressedAction( btn, true ) );
  12. am.put( "number-released", new NumberKeyPressedAction( btn, false ) );
  13. GridBagConstraints gbc_btn = new GridBagConstraints();
  14. // gbc_btnEquals.anchor = GridBagConstraints.WEST;
  15. gbc_btn.fill = GridBagConstraints.BOTH;
  16. gbc_btn.insets = new Insets( 0, 0, 5, 5 );
  17. gbc_btn.gridheight = h;
  18. gbc_btn.gridwidth = w;
  19. gbc_btn.gridx = x;
  20. gbc_btn.gridy = y;
  21. frame.getContentPane().add( btn, gbc_btn );
  22. btn.setBackground( new Color( 225, 225, 225 ) );
  23. btn.setBorder( BorderFactory.createLineBorder( Color.BLACK ) );

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-xml-xam-ui

  1. /**
  2. * Creates new form SearchControlPanel.
  3. */
  4. public SearchControlPanel() {
  5. initComponents();
  6. nextButton.setEnabled(false);
  7. prevButton.setEnabled(false);
  8. closeButton.addActionListener(this);
  9. nextButton.addActionListener(this);
  10. prevButton.addActionListener(this);
  11. resetButton.addActionListener(this);
  12. searchField = new SearchFieldPanel();
  13. searchField.addSearchListener(this);
  14. fieldPanel.add(searchField, BorderLayout.CENTER);
  15. //IZ 91545
  16. findAcion = new FindAction();
  17. nextButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
  18. put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "findNext"); //NOI18N
  19. nextButton.getActionMap().put("findNext", findAcion); //NOI18N
  20. prevButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
  21. put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, KeyEvent.SHIFT_MASK), "findPrevious"); //NOI18N
  22. prevButton.getActionMap().put("findPrevious", findAcion);
  23. }

代码示例来源:origin: stackoverflow.com

  1. GridLayout layout = new GridLayout(0, 4);
  2. jPanel1.setLayout(layout);
  3. Action action = new AbstractAction() {
  4. @Override
  5. public void actionPerformed(ActionEvent e) {
  6. System.err.println("Event generated for source: "+((JButton)e.getSource()).getName());
  7. }
  8. };
  9. for(int i=0 ;i<4; i++)
  10. for(int j=0; j<4; j++)
  11. {
  12. String key = ""+(char)('A' + i * 4 + j);
  13. JButton button = new JButton(key);
  14. button.setName(key);
  15. button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), "doAction");
  16. button.getActionMap().put("doAction", action);
  17. jPanel1.add(button);
  18. }

代码示例来源:origin: stackoverflow.com

  1. MyAction myaction = new MyAction("1");
  2. JButton b = new JButton(myaction);
  3. b.setAction(myaction);
  4. b.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "one");
  5. b.getActionMap().put("one", myaction);

代码示例来源:origin: MegaMek/megamek

  1. ActionMap amap = butYes.getActionMap();
  2. imap.put(ks, YESACTION);
  3. amap.put(YESACTION, yesAction);
  4. amap = butNo.getActionMap();
  5. imap.put(ks, NOACTION);
  6. amap.put(NOACTION, noAction);

代码示例来源:origin: stackoverflow.com

  1. AbstractAction action = new AbstractAction() {
  2. @Override
  3. public void actionPerformed(ActionEvent e) {
  4. if(e.getSource() instanceof JButton){
  5. JButton button = (JButton) e.getSource();
  6. button.doClick();
  7. } else if(e.getSource() instanceof JComponent){
  8. JComponent component = (JComponent) e.getSource();
  9. component.transferFocus();
  10. }
  11. }
  12. };
  13. JTextField textField1 = new JTextField();
  14. textField1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
  15. textField1.getActionMap().put("TransferFocus", action);
  16. JTextField textField2 = new JTextField();
  17. textField2.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
  18. textField2.getActionMap().put("TransferFocus", action);
  19. JButton button1= new JButton();
  20. button1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
  21. button1.getActionMap().put("Enter", action);

代码示例来源:origin: kaikramer/keystore-explorer

  1. jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
  2. CANCEL_KEY);
  3. jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {
  4. private static final long serialVersionUID = 1L;

代码示例来源:origin: kaikramer/keystore-explorer

  1. jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
  2. CANCEL_KEY);
  3. jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {
  4. private static final long serialVersionUID = 1L;

代码示例来源:origin: kaikramer/keystore-explorer

  1. jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
  2. CANCEL_KEY);
  3. jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {
  4. private static final long serialVersionUID = 1L;

代码示例来源:origin: stackoverflow.com

  1. public static void bindKeyStroke(final JButton btn, String ks) {
  2. final ActionListener[] alist = btn.getActionListeners();
  3. if (alist.length != 0) {
  4. AbstractAction action = new AbstractAction(btn.getText(), btn.getIcon()) {
  5. @Override
  6. public void actionPerformed(ActionEvent e) {
  7. for (ActionListener al : alist) {
  8. ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), Action.ACCELERATOR_KEY);
  9. al.actionPerformed(ae);
  10. }
  11. }
  12. };
  13. KeyStroke keyStroke = KeyStroke.getKeyStroke(ks);
  14. btn.setAction(action);
  15. btn.getActionMap().put(Action.ACCELERATOR_KEY, action);
  16. btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, Action.ACCELERATOR_KEY);
  17. }
  18. }

相关文章

JButton类方法