Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用操作更新另一个类中的实例变量_Java_Swing_Action_Key Bindings - Fatal编程技术网

Java 使用操作更新另一个类中的实例变量

Java 使用操作更新另一个类中的实例变量,java,swing,action,key-bindings,Java,Swing,Action,Key Bindings,所以我尝试使用键绑定,action map的put()方法接受一个action和一个string参数 /* all declartion is above * the class extends JPanel so the keyword "this" can be used * xlist is an ArrayList of Integers * keyActionRight ka = new keyActionRight(x); is declared abo

所以我尝试使用键绑定,action map的put()方法接受一个action和一个string参数

/* all declartion is above
     * the class extends JPanel so the keyword "this" can be used
     * xlist is an ArrayList of Integers
     * keyActionRight ka = new keyActionRight(x); is declared above, where x is a global int
     * this is part of the keyBindingsTest class */

    xlist.add(x); 
    im = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");

    am = this.getActionMap();
    am.put("right", ka);
    System.out.println(ka.getNextX(xlist)); //Any way for this to be called just like if I printed in the actionPerformed of the action class?
这是keyActionRight类。当您扩展AbstractAction时得到一个action时,它就是一个action:

public class keyActionRight extends 
AbstractAction
{
    private int x; 
    private ArrayList<Integer> xlist;
    public keyActionRight(int x)
    {
        this.x = x;
        xlist = new ArrayList<Integer>(); 
        xlist.add(x);  
    }

    public int getNextX(ArrayList<Integer> x)
    {
        x = xlist; 
        return x.get(0);
    }

    public void actionPerformed(ActionEvent e)
    {
        if(x != 440)
        {
            x++; //this incrementing works fine
            xlist.add(0, x); //this updates xlist fine
        }  
    }
}
public类keyActionRight扩展
抽象行为
{
私人INTX;
私有数组列表xlist;
公钥操作权(int x)
{
这个.x=x;
xlist=newarraylist();
xlist.add(x);
}
public int getNextX(ArrayList x)
{
x=xlist;
返回x.get(0);
}
已执行的公共无效操作(操作事件e)
{
如果(x!=440)
{
x++;//这种递增方式很好
xlist.add(0,x);//这会很好地更新xlist
}  
}
}
基本上,只要我按下或按住向右箭头键,就可以更新keyBindingsTest类中的实例变量x。当我这样做时,Action类中的x更新得很好(我打印出来了,它可以正常工作)。已经指出了为什么它不更新——它在print语句中只被调用一次。我想知道是否有一种方法可以通过单独的类来完成这个动作,或者我是否需要采取不同的方法

我可以尝试在keyBindingsTest类中进行操作,但上次尝试时,这给了我奇怪的错误。任何帮助都将不胜感激


谢谢

您有错误的假设:

xlist.add(x); 
im = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");

am = this.getActionMap();
am.put("right", ka);

// **** the comment below is incorrect ****
//only prints out zero - should print out ascending values of x as I hold down the right arrow key
System.out.println(ka.getNextX(xlist));  
您所做的假设是在调用键绑定操作时调用println,但事实并非如此。println只在创建密钥绑定时调用一次。唯一被重复调用的代码是动作的actionPerformed方法中的代码,即响应事件而调用的代码


如果要多次调用代码并响应事件,则必须将其放置在事件侦听器中,而不是侦听器的创建代码中。

将您的整个错误消息共享给我们。没有错误消息。keyBindingsTest中的x不是递增的。或者在本例中,我尝试使用ArrayList,因此ArrayList不会更新。为什么您希望println打印多次?调用操作时不调用它,但在创建绑定时只调用一次。如果希望它打印多次,则需要在事件中调用它,例如在操作的actionPerformed方法中调用。是的,这是有意义的。应该想到这个lol。谢谢。如果你改变你的问题,请回复我的答案,否则这是不公平的。要回答您的问题(如我在回答中所述),在激活操作时调用代码,必须从侦听器中调用代码。这差不多就是故事的结尾了,这对你不起作用,你还没有解释原因。另外,请从帮助站点阅读此部分:。