在SO和@kleopatra的帮助下,我立即在下面获得了将Ctrl-Shift-U分配给JButton的代码。 (我在不应该向我明确说明必须匹配的内容的地方使用了CAPS。)

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import static java.awt.event.InputEvent.SHIFT_DOWN_MASK;
import static java.awt.event.KeyEvent.VK_U;
import javax.swing.*;
import static javax.swing.KeyStroke.getKeyStroke;

public class FirstTry {
  private static JButton MYACTIONBUTTON;
  private static JFrame frame;
  private static JPanel panel;

  public FirstTry(){
    Action MYACTION = new AbstractAction(){
      @Override
      public void actionPerformed(ActionEvent e){
          JOptionPane.showMessageDialog(null,"do this");
      }};

    MYACTIONBUTTON = new JButton(MYACTION);
    MYACTIONBUTTON.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                          .put(getKeyStroke(VK_U, CTRL_DOWN_MASK | SHIFT_DOWN_MASK),
                                      "MAKE_THESE_MATCH");
    MYACTIONBUTTON.getActionMap().put("MAKE_THESE_MATCH", MYACTION);

    frame = new JFrame();
    panel = new JPanel();
    panel.add(MYACTIONBUTTON);
    frame.add(panel);
    frame.setVisible(true);
    frame.pack();
}

public static void main(String[] args) {
  new FirstTry();
}


下次我想进行类似的键绑定时,要经历很多工作,因此我认为应该可以推断逻辑以形成abstract class,从而轻松地为操作分配击键(例如Ctrl-X)(例如,通过如下语句执行“按下Ctrl-X”,如下所示:

button = new KeyBoundButton("WHATEVER", VK_X, CTRL_DOWN_MASK)
{
  @Override
  public void actionPerformed(ActionEvent e)
  {
    JOptionPane.showMessageDialog(null,"Ctrl-X was pressed!");
  }
};


我已经(半)整天都在工作,却一无所获。我有3个类:main,UI和KeyBoundButton。我承认我在头上。

public class NewMain {

  static UI ui;

  public static void main(String[] args)
  {
    java.awt.EventQueue.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        ui = new UI();
      }
    });
  }
}


用户界面:

import java.awt.event.ActionEvent;
import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import static java.awt.event.KeyEvent.VK_X;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class UI {

  static JFrame frame;
  static JPanel panel;
  static KeyBoundButton button;

  UI(){
    frame = new JFrame();
    panel = new JPanel();

    button = new KeyBoundButton("WHATEVER", VK_X, CTRL_DOWN_MASK)
    {
      @Override
      public void actionPerformed(ActionEvent e)
      {
        JOptionPane.showMessageDialog(null,"Ctrl-X was pressed!");
      }
    };

    panel.add(button);
    frame.add(panel);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    frame.pack();
  }
}


KeyBoundButton:

import java.awt.event.ActionEvent;
import javax.swing.*;
import static javax.swing.KeyStroke.getKeyStroke;

public abstract class KeyBoundButton extends JButton{

  public abstract void actionPerformed(ActionEvent e);

  public KeyBoundButton(String actionMapKey, int key, int mask)
  {
    Action myAction = new AbstractAction()
    {
      @Override
      public void actionPerformed(ActionEvent e)
      { // to be overridden
      }
    };

     new JButton(myAction);

    getInputMap(WHEN_IN_FOCUSED_WINDOW)
                  .put(getKeyStroke(key, mask),actionMapKey);
    getActionMap().put(                        actionMapKey, myAction);
  }
}


如上(和下图)所示,我是否在使此类变得容易进行键绑定方面完全关闭了?还是只是在没有足够的键绑定知识的情况下进行了这么一厢情愿的想法?

button = new KeyBoundButton("WHATEVER", VK_X, CTRL_DOWN_MASK)
{
  @Override
  public void actionPerformed(ActionEvent e)
  {
    JOptionPane.showMessageDialog(null,"Ctrl-X was pressed!");
  }
};

最佳答案

您无需在new JButton(myAction);旁添加KeyBoundButton,而使用setAction(myAction)

第二个按钮的创建与任何内容都没有关系,实际上,在调用new JButton(myAction)之后,该对象立即可以进行垃圾回收

我不久前写了一个实用程序类,它的功能类似,但是意味着我不需要为要使用的每个类创建“自定义”类,因此我可以简单地将其传递给Component,名称,KeyStrokeAction(以及可选的焦点级别),这使生活变得更加简单

10-07 23:29