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

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

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

JButton.getActionMap介绍

暂无

代码示例

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

// create an Action doing what you want
Action action = new AbstractAction("doSomething") {

  @Override
  public void actionPerformed(ActionEvent e) {
    System.out.println("triggered the action");
  }

};
// configure the Action with the accelerator (aka: short cut)
action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S"));

// create a button, configured with the Action
JButton toolBarButton = new JButton(action);
// manually register the accelerator in the button's component input map
toolBarButton.getActionMap().put("myAction", action);
toolBarButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
    (KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "myAction");

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

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

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

JButton clear = new JButton("Clear");

InputMap im = clear .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = clear .getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "clear");
am.put("clear", new ClearFieldsAction());

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

public static void fixCancelButton(final javax.swing.JButton cancelButton)
{
 cancelButton.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).
 put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0),
   "Cancel");
 cancelButton.getActionMap().put("Cancel",
   new javax.swing.AbstractAction()
 {
  public void actionPerformed(java.awt.event.ActionEvent evt)
  {
   cancelButton.doClick();
  }
 });
}

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

public static void fixOKButton(final javax.swing.JButton okButton)
{
 okButton.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).
 put(javax.swing.KeyStroke.getKeyStroke(
   java.awt.event.KeyEvent.VK_ENTER, 0), "OK");
 okButton.getActionMap().put("OK",
   new javax.swing.AbstractAction()
 {
  public void actionPerformed(java.awt.event.ActionEvent evt)
  {
   okButton.doClick();
  }
 });
}

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

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

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

NumberAction action = new NumberAction(1);
JButton btn = new JButton(action);
InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = btn.getActionMap();

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "Number1");
am.put("Number1", action);

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

@Override
public void actionPerformed(ActionEvent e) {
  JButton b=  button;
  if (b!=null && isEnabled()) {
    button.getActionMap().get(type).actionPerformed(e);
  }
}

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

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

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

KeyStroke keySave = KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK); 
Action performSave = new AbstractAction("Save") {  
  public void actionPerformed(ActionEvent e) {     
     //do your save
     System.out.println("save");
  }
}; 
JButton b1 = new JButton(performSave); 
b1.getActionMap().put("performSave", performSave);
b1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keySave, "performSave"); 

KeyStroke keyExit = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK); 
Action performExit = new AbstractAction("Exit") {  
  public void actionPerformed(ActionEvent e) {     
    //exit
    System.out.println("exit");
  }
}; 
JButton b2 = new JButton(performExit); 
b2.getActionMap().put("performExit", performExit);
b2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyExit, "performExit");

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

private void createButton( int x, int y, int h, int w, String actionCommand, String name, int... keys ) {
 nAction na = new nAction( actionCommand );
 JButton btn = new JButton( na );
 btn.setName( name );
 InputMap im = btn.getInputMap( WHEN_IN_FOCUSED_WINDOW );
 ActionMap am = btn.getActionMap();
 for ( int virtualKey : keys ) {
   im.put( KeyStroke.getKeyStroke( virtualKey, 0, false ), "number-pressed" );
   im.put( KeyStroke.getKeyStroke( virtualKey, 0, true ), "number-released" );
 }
 am.put( "number-pressed", new NumberKeyPressedAction( btn, true ) );
 am.put( "number-released", new NumberKeyPressedAction( btn, false ) );
 GridBagConstraints gbc_btn = new GridBagConstraints();
 // gbc_btnEquals.anchor = GridBagConstraints.WEST;
 gbc_btn.fill = GridBagConstraints.BOTH;
 gbc_btn.insets = new Insets( 0, 0, 5, 5 );
 gbc_btn.gridheight = h;
 gbc_btn.gridwidth = w;
 gbc_btn.gridx = x;
 gbc_btn.gridy = y;
 frame.getContentPane().add( btn, gbc_btn );
 btn.setBackground( new Color( 225, 225, 225 ) );
 btn.setBorder( BorderFactory.createLineBorder( Color.BLACK ) );

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

/**
 * Creates new form SearchControlPanel.
 */
public SearchControlPanel() {
  initComponents();
  nextButton.setEnabled(false);
  prevButton.setEnabled(false);
  closeButton.addActionListener(this);
  nextButton.addActionListener(this);
  prevButton.addActionListener(this);
  resetButton.addActionListener(this);
  searchField = new SearchFieldPanel();
  searchField.addSearchListener(this);
  fieldPanel.add(searchField, BorderLayout.CENTER);
  
  //IZ 91545
  findAcion = new FindAction();
  nextButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
      put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "findNext"); //NOI18N
  nextButton.getActionMap().put("findNext", findAcion); //NOI18N
  prevButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).
      put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, KeyEvent.SHIFT_MASK), "findPrevious"); //NOI18N
  prevButton.getActionMap().put("findPrevious", findAcion);
}

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

GridLayout layout = new GridLayout(0, 4);
   jPanel1.setLayout(layout);
   Action action = new AbstractAction() {
     @Override
     public void actionPerformed(ActionEvent e) {
       System.err.println("Event generated for source: "+((JButton)e.getSource()).getName());
     }
   };
   for(int i=0 ;i<4; i++)
     for(int j=0; j<4; j++)
     {
       String key = ""+(char)('A' + i * 4 + j);
       JButton button = new JButton(key);
       button.setName(key);
       button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), "doAction");
       button.getActionMap().put("doAction", action);
       jPanel1.add(button);
     }

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

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

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

ActionMap amap = butYes.getActionMap();
imap.put(ks, YESACTION);
amap.put(YESACTION, yesAction);
amap = butNo.getActionMap();
imap.put(ks, NOACTION);
amap.put(NOACTION, noAction);

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

AbstractAction action = new AbstractAction() {
  @Override
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() instanceof JButton){
      JButton button = (JButton) e.getSource();
      button.doClick();                
    } else if(e.getSource() instanceof JComponent){
      JComponent component = (JComponent) e.getSource();
      component.transferFocus();
    }
  }
};

JTextField textField1 = new JTextField();
textField1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
textField1.getActionMap().put("TransferFocus", action);

JTextField textField2 = new JTextField();
textField2.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus");
textField2.getActionMap().put("TransferFocus", action);

JButton button1= new JButton();
button1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
button1.getActionMap().put("Enter", action);

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

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

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

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

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

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

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

public static void bindKeyStroke(final JButton btn, String ks) {
  final ActionListener[] alist = btn.getActionListeners();
  if (alist.length != 0) {
    AbstractAction action = new AbstractAction(btn.getText(), btn.getIcon()) {
      @Override
      public void actionPerformed(ActionEvent e) {
        for (ActionListener al : alist) {
          ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), Action.ACCELERATOR_KEY);
          al.actionPerformed(ae);
        }
      }
    };

    KeyStroke keyStroke = KeyStroke.getKeyStroke(ks);
    btn.setAction(action);
    btn.getActionMap().put(Action.ACCELERATOR_KEY, action);
    btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, Action.ACCELERATOR_KEY);
  }
}

相关文章

JButton类方法