Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.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 在同一个类中,我如何在同一件事情上一遍又一遍地执行actionListener?_Java_Swing_User Interface - Fatal编程技术网

Java 在同一个类中,我如何在同一件事情上一遍又一遍地执行actionListener?

Java 在同一个类中,我如何在同一件事情上一遍又一遍地执行actionListener?,java,swing,user-interface,Java,Swing,User Interface,如果我想一次又一次地对按钮执行ActionListener,这样它就不会再给我相同的ans了,我该怎么办 例如: down = new JButton("DOWN-1"); down.setSize(down.getPreferredSize()); down.setLocation(100,200); down.addActionListener(this); left=new JButton("LEFT-1"); left.setSize(left.getPreferredSize());

如果我想一次又一次地对按钮执行
ActionListener
,这样它就不会再给我相同的ans了,我该怎么办

例如:

down = new JButton("DOWN-1");
down.setSize(down.getPreferredSize());
down.setLocation(100,200);
down.addActionListener(this);
left=new JButton("LEFT-1");
left.setSize(left.getPreferredSize());
left.setLocation(100,250);
left.addActionListener(this);
right=new JButton("RIGHT-1");
right.setSize(right.getPreferredSize());
right.setLocation(100,300);
right.addActionListener(this);
up1=new JButton("UP-2");
up1.setSize(up1.getPreferredSize());
up1.setLocation(550,150);
up.addActionListener(this);

@Override
public void actionPerformed(ActionEvent a)
{
    int counter=370;

    if (a.getSource()==up) {
        System.out.println(counter);

        x=250+62+62;
        y=60+62+62+62+62+62;
        b1.setLocation(x,counter-62);
        l19.setLocation(x,counter);
    }   
}

在这里,我想一次又一次地使用up按钮,但它不起作用…

而您的问题非常令人困惑。我想我已经领会到了它的一些含义。“…如果我想一次又一次地在按钮上执行ActionListener,…”。我的猜测是,你的意思是你希望能够多次使用按钮

@Override
public void actionPerformed(ActionEvent a)
{
    int counter=370;

    if (a.getSource()==up) {
        System.out.println(counter);

        x=250+62+62;
        y=60+62+62+62+62+62;
        b1.setLocation(x,counter-62);
        l19.setLocation(x,counter);
    }   
}
这就是我在你的代码中看到的,它会让你认为你不能多次使用它

@Override
public void actionPerformed(ActionEvent a)
{
    int counter=370;

    if (a.getSource()==up) {
        System.out.println(counter);

        x=250+62+62;
        y=60+62+62+62+62+62;
        b1.setLocation(x,counter-62);
        l19.setLocation(x,counter);
    }   
}
发生的事情是,每次点击按钮,位置总是设置为相同的位置。我的初始位置可能不同,这就是为什么它在第一次单击时似乎工作(位置将更改)。但在那之后,每次点击都会指向同一个位置,所以无论你希望移动什么,都不会

虽然我不知道你的代码是做什么的,但是从你的最小的“解释”(如果你可以这样称呼它的话)。我可以提个建议。似乎
计数器
是偏移因子,因此您可能需要给
计数器
一个全局范围,并在每次单击按钮时更改其值。像这样的

int counter = 370;

@Override
public void actionPerformed(ActionEvent a)
{
    if (a.getSource()==up) {
        counter -= 62;         // this is where you change the value of counter

        System.out.println(counter);

        x=250+62+62;           // I have no idea what this is for
        y=60+62+62+62+62+62;   // or this, so I won't comment
        b1.setLocation(x,counter);   // just use the new counter value
        l19.setLocation(x,counter);
    }   
}

你的问题不清楚。您也不会执行侦听器,当发生某些事情时会调用侦听器。噢,哇,我不小心删除了我的评论,但问题是:
up.addActionListener(此)应该是
up1.addActionListener(这个)我知道。。。。但是我想一次又一次地执行它……@zaplYou将您的侦听器附加(添加)到
up
。它是什么?它的定义在哪里?另外,应用程序的整体结构也不清楚。您是否创建
JForm
(或其他顶级容器)?向我们展示您的整个GUI代码。