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

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

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

JButton.getInputMap介绍

暂无

代码示例

代码示例来源: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

public static void main(String[] args)
{
  JButton first = new JButton("button");
  System.out.println(first.getInputMap().getParent());

  InputMap im = (InputMap) UIManager.get("Button.focusInputMap");
  System.out.println(im);
}

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

JButton press = new JButton("Press me");
InputMap im = press.getInputMap(WHEN_FOCUSED);

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), "pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "released");

代码示例来源: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: 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: sing-group/GC4S

/**
 * Returns the buttons panel of the dialog.
 * 
 * @return the buttons panel of the dialog.
 */
protected Component getButtonsPane() {
  final JPanel buttonsPanel = new JPanel(new FlowLayout());
  
  okButton = new JButton("Ok", ICON_ACCEPT);
  okButton.setEnabled(false);
  okButton.setToolTipText("Accept");
  okButton.addActionListener(this::onOkButtonEvent);
  
  cancelButton = new JButton("Cancel", ICON_CANCEL);
  cancelButton.setToolTipText("Cancel");
  cancelButton.addActionListener(event -> {
    canceled = true;
    dispose();
  });
  buttonsPanel.add(okButton);
  buttonsPanel.add(cancelButton);
  
  getRootPane().setDefaultButton(okButton);
  InputMap im = okButton.getInputMap();
  im.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
  im.put(KeyStroke.getKeyStroke("released ENTER"), "released");
  
  return buttonsPanel;
}

代码示例来源: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: com.numdata/numdata-swing

/**
 * Add buttons to content pane.
 *
 * @param contentPane Content pane to add buttons to.
 * @param buttons     Buttons argument from constructor.
 */
private void addButtons( final StandardContentPane contentPane, final int buttons )
{
  final ResourceBundle res = _res;
  if ( ( buttons & OK_BUTTON ) != 0 )
  {
    final JButton button = contentPane.addButton( res, OK_ACTION, this );
    _okButton = button;
    final InputMap inputMap = button.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW );
    inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK, false ), "pressed" );
    inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK, true ), "released" );
  }
  if ( ( buttons & CANCEL_BUTTON ) != 0 )
  {
    final JButton button = contentPane.addButton( res, CANCEL_ACTION, this );
    final InputMap inputMap = button.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW );
    inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0, false ), "pressed" );
    inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0, true ), "released" );
  }
  if ( ( buttons & APPLY_BUTTON ) != 0 )
  {
    contentPane.addButton( res, APPLY_ACTION, this );
  }
}

代码示例来源: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

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: 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

.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap amap = butYes.getActionMap();
imap.put(ks, YESACTION);
imap = butNo.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
amap = butNo.getActionMap();
imap.put(ks, 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: 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类方法