0

我有一个 JLabel,上面有键绑定操作。我已经为一些动作定义了一些代码,但是,唉,还有其他 JLabels、JPanel 和方法中的其他东西(这是在 main() 中)我希望我的动作愚弄。

我试图将动作更改为带参数,但没有成功,我怎样才能让我的动作带参数进行操作?有什么办法吗?我已经看过了,但这非常具体,我看不到几个很好的例子。

这是我的代码的一个很好的板块:

    /*Bunch of stuff I want my actions to interact with above - another JLabel, a JPanel*/
    ImageIcon cursor = new ImageIcon("cursor.gif");
    JLabel cursorlbl = new JLabel("", cursor, JLabel.CENTER);
    Action goRight = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("lol");

        }
    };

    Action goLeft = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("lol2");
        }
    };

    Action goUp = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent arg0) {


        }
    };

    Action goDown = new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println("lol2");

        }
    };


    cursorlbl.setFocusable(true);
    cursorlbl.getInputMap().put(KeyStroke.getKeyStroke("RIGHT"),
            "pressed right");
    cursorlbl.getInputMap().put(KeyStroke.getKeyStroke("LEFT"),
            "pressed left");
    cursorlbl.getActionMap().put("pressed right", goRight);
    cursorlbl.getActionMap().put("pressed left", goLeft);
4

3 回答 3

2

您可以将每个操作声明为子类(这无论如何都会开始分离 MVC),并且您要操作父类中的字段的每个项目。例子:

// Parent Class
public class ParentClass{
//Field you want to mess with in your action
   JLabel cursorlbl = new JLabel("");

    // Action that does things
    public class MoveAction extends AbstractAction{
        char direction;

        //Constructor for action
        public MoveAction(char direction){
            this.direction = direction;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            int change = 0;

            // Figure out how you'll be changing the variable
            if(direction == 'u' || direction == 'r'){
                change = 1;
            } else{
                change = -1;
            }

            // Apply the change to the correct variable
            if(direction == 'u' || direction =='d'){
                cursy += change;
            } else{
                cursx += change;
            }

            //Example how you can access the parent class's fields
            cursorlbl.setLocation(cursx, cursy);
        }       
    }
}

然后要设置您的操作,您只需创建子类的实例:

    contentArea.getActionMap().put("pressed right", new MoveAction('r'));
    contentArea.getActionMap().put("pressed left", new MoveAction('l'));
于 2012-07-12T21:50:44.400 回答
0

将您希望在您的操作中看到的其他组件声明为final. 这将使行动看到他们。

更多信息在这里

于 2012-07-12T17:35:56.227 回答
0

您应该能够像这样传递它们:

// note that the JLabel is now final
final JLabel cursorlbl = new JLabel("", cursor, JLabel.CENTER);
Action goRight = new AbstractAction() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        System.out.println(cursorlbl.getText()); // has access to JLabel because it's scoped to the method/class

    }
};

请注意,这样做可能会导致一些维护问题,您应该尝试记录未来开发人员可能不清楚的事情(以及两周后的您自己!)

于 2012-07-12T17:36:32.653 回答