KeyBinding在JForm中不起作用

时间:2018-07-26 21:15:38

标签: java swing jframe key-bindings

我在JForm UI中有左右按钮,我想将“ A”和“ D”绑定到这些按钮。我一直在尝试关注此处的其他帖子,但是我似乎仍然无法使其正常工作。

这是我的按钮代码:

private void clickRightActionPerformed(java.awt.event.ActionEvent evt) {                                           
    this.evt = evt;
    try {
        lastScore = cellData(sheetNum, rowNum, xls.getAbsolutePath());
        nextScore = cellData(sheetNum, rowNum + 1, xls.getAbsolutePath());
        System.out.println();
        scoreSet(xls, sheetNum, rowNum - 2, "n", lastScore);
    } catch (Exception ex) {
        Logger.getLogger(ReviewingWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
    giveScore.setText("SCORE = " + nextScore);
}   

private void clickLeftActionPerformed(java.awt.event.ActionEvent evt) {                                          
    SetImageSize(rowNum, reviewImages, pic);
    try {
        if (rowNum < imageCount - 1) {
            lastScore = cellData(sheetNum, rowNum + 2, xls.getAbsolutePath());
            nextScore = cellData(sheetNum, rowNum + 1, xls.getAbsolutePath());
        scoreSet(xls, sheetNum, rowNum, "p", lastScore);
        } else {
            lastScore = cellData(sheetNum, rowNum, xls.getAbsolutePath());
            nextScore = cellData(sheetNum, 1, xls.getAbsolutePath());
        }
    } catch (Exception ex) {
        Logger.getLogger(ReviewingWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
    giveScore.setText("SCORE = " + nextScore);
}     

这是我的keyBinding代码:

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {

    int keyPress = e.getKeyCode();
    switch(keyPress) { 
        case KeyEvent.VK_D:
            clickRightActionPerformed(evt);
            break;
        case KeyEvent.VK_A:
            clickLeftActionPerformed(evt);
            break;
    }

@Override
public void keyReleased(KeyEvent e) {

}

1 个答案:

答案 0 :(得分:0)

您需要大量阅读,我建议从以下开始:

您要做的第一件事是将功能分离。这意味着“向左移动”和“向右移动”操作与调用它们的方式无关,对我而言,它以interface开头,它描述了已提供的基本合同...

public interface MovementController {
    public void moveRight();
    public void moveLeft();
}

(是的,如果是我,我会有左右interface,但我正试图保持简单;))

接下来,您要设计一些Action可以与此interface进行交互的

public class RightAction extends AbstractAction {

    private Controller controller;

    public RightAction(Controller controller) {
        this.controller = controller;
        // Button text
        putValue(NAME, "Right");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        controller.moveRight();
    }

}

public class LeftAction extends AbstractAction {

    private Controller controller;

    public LeftAction(Controller controller) {
        this.controller = controller;
        // Button text
        putValue(NAME, "Left");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        controller.moveLeft();
    }

}

最后,我们需要完全了解它...

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class Test extends JPanel implements Controller {

    public Test() {
        LeftAction leftAction = new LeftAction(this);
        RightAction rightAction = new RightAction(this);

        JButton leftButton = new JButton(leftAction);
        JButton rightButton = new JButton(rightAction);

        // Add buttons to the UI...

        InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "left");
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0), "right");

        am.put("left", leftAction);
        am.put("right", rightAction);
    }

    @Override
    public void moveRight() {
        try {
            lastScore = cellData(sheetNum, rowNum, xls.getAbsolutePath());
            nextScore = cellData(sheetNum, rowNum + 1, xls.getAbsolutePath());
            System.out.println();
            scoreSet(xls, sheetNum, rowNum - 2, "n", lastScore);
        } catch (Exception ex) {
            Logger.getLogger(ReviewingWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        giveScore.setText("SCORE = " + nextScore);
    }

    @Override
    public void moveLeft() {
        SetImageSize(rowNum, reviewImages, pic);
        try {
            if (rowNum < imageCount - 1) {
                lastScore = cellData(sheetNum, rowNum + 2, xls.getAbsolutePath());
                nextScore = cellData(sheetNum, rowNum + 1, xls.getAbsolutePath());
                scoreSet(xls, sheetNum, rowNum, "p", lastScore);
            } else {
                lastScore = cellData(sheetNum, rowNum, xls.getAbsolutePath());
                nextScore = cellData(sheetNum, 1, xls.getAbsolutePath());
            }
        } catch (Exception ex) {
            Logger.getLogger(ReviewingWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        giveScore.setText("SCORE = " + nextScore);
    }

}

作为常见建议,最好将UI基于JPanel,而不是JFrameJFrame是一个复合组件,这只会增加整个管理的复杂性,以JPanel作为基础组件,可以为您提供更大的自由度,并可以将其添加到以后需要的任何容器中