需要类以方便键绑定

时间:2015-04-02 21:12:55

标签: java abstract-class key-bindings

在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,这样可以轻松地分配键击(例如,{{ 1}})通过以下语句执行操作(例如,“按下Ctrl-X”,如图所示):

Ctrl-X

我已经全天(半天)工作了,而且无处可去。我有3个类:main,UI和KeyBoundButton。我承认我在我的头上。

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

UI:

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();
      }
    }); 
  }
}

KeyBoundButton:

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();
  }
}

如上所示(及以下),我是否完全支持这个类以实现轻松的键绑定?或者只是如此多的一厢情愿的想法与其他事情的关键绑定知识不足?

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);
  }
}

2 个答案:

答案 0 :(得分:1)

new JButton(myAction);旁边不需要KeyBoundButton,而是使用setAction(myAction)

创建第二个按钮与任何事情无关,事实上,在您调用new JButton(myAction)之后,该对象就有资格进行垃圾回收

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

答案 1 :(得分:0)

@MadProgrammer让我开始;这是什么工作。对原始代码的更改标有///////////////。 (我不确定为什么原来的public abstract void actionPerformed(ActionEvent e)行没有用......看起来不错。[?])

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

public abstract class KeyBoundButton extends JButton{

  public abstract void doThis(ActionEvent e); //////////////////////

  public KeyBoundButton(String actionMapKey, int key, int mask)
  {
    Action myAction = new AbstractAction()
    {

      @Override
      public void actionPerformed(ActionEvent e)
      {
        doThis(e); ///////////////////////////
      }
    };    

    setAction(myAction); //////////////////////////////

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

我最初的意图是(正如Mad也暗示的那样)在合理范围内为任何组件做这项工作;现在就到了。我可能会回来寻求更多帮助。