为什么我的ActionListener不断重复?

马特·佩恩(Matt Payne)

我制作了一个JButton并添加了一个“ AddActionListener”,但它多次重复执行相同的操作(每次按此按钮2-6次)。

我确定最终会找到它的,但是您能在这段代码中看到任何能够说明其重复原因的内容吗?

它调用的任何方法都不能使动作重复。另外,我可以从其他地方调用这些方法,这没关系。

编辑:这是问题类的完整代码:

这是问题类导入的完整代码

 public class Act
 {
private String title;
private Vector<Person> victims;
private Person victim;
private Person performer;
private Control control;
private Place oldPlace;
private JPanel oldPlacePanel;

public Cult theCult;
public Audience theAudience;
public Society theSociety;

private String description;

private JPanel actPanel;
private JTextArea actTextArea;
private JLabel actLabel;
//private JButton performButton;

private JComboBox victimsList;
private JLabel victimLabel;

private CultLeader leader;

private int charismaCost;
private Trait dominantTrait;
private int minimumPoints;

private boolean group;
private boolean individual;

private Trait streetSmart;
private Trait bookSmart;
private Trait cruel;
private Trait paranoid;
private Trait artistic;
private Trait powerHungry;
private Trait compassionate;
private Trait political;
private Trait sexual;
private Trait needy;
private Trait crazy;
private Trait adventurous;
private Trait practical;
private Trait sneaky;

/**
 * Constructor
 */
public Act(String theTitle, CultLeader theLeader, Control theControl)
{
    leader = theLeader;
    title = theTitle;
    control = theControl;

    theAudience = control.theAudience;
    theCult = control.theCult;
    theSociety = control.theSociety;

    actTextArea = new JTextArea();
    actPanel = new JPanel();
    actLabel = new JLabel(title);

    victimsList = new JComboBox<String>();
    victimLabel = new JLabel();

    if (title == "Pamphlet")
    {
        makePamphlet();
    }
    else if (title == "Speech")
    {
        makeSpeech();

    }

    System.out.println("You created a " + title);
}

/**
 * This method turns THIS act (the currently-being-constructed Act object)
 * into a generic Pamphlet
 */
public void makePamphlet()
{
    charismaCost = 5;
    dominantTrait = new Trait("Paranoid");
    individual = true;
    group = false;

    description = "blah blah";
}   


/**
 * This method turns THIS act (the currently-being-constructed Act object)
 * into a generic Speech
 */
public void makeSpeech()
{
    charismaCost = 5;
    dominantTrait = new Trait("Crazy");
    individual = false;
    group = true;


    description = "blah blah";

}    

/**
 * Returns the title of this Act
 */
public String getTitle()
{
    return title;
}

/**
 * find out if this Act is meant for groups
 */
public boolean isGroup()
{
    return group;
}

/**
 * find out if this Act is meant for individuals
 */
public boolean isIndividual()
{
    return individual;
}

/**
 * This method will DISPLAY information about the Act in a JPanel in the bottom container in the Frame
 * It will show the description and the intended victims, then a Perform button.
 * 
 * There will be TWO displayAct methods... one which takes a single person as a victim
 * and a second, almost identical, which takes a Vector<Person>
 */
public JPanel displayAct(Person theVictim)
{
    victim = theVictim;

    actPanel.setLayout(new GridLayout(5,1));

    actPanel.removeAll();

    actTextArea.setText(description);
    actPanel.add(actLabel);
    actPanel.add(actTextArea);

    victimLabel.setText(theVictim.getName());

    actPanel.add(victimLabel);

    JButton performButton2 = new JButton("Perform Act");
    //setupSingleButton();
    //testButton();

    performButton2.setText("Give this " + title + " to " + victim.getName());
    performButton2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            System.out.println("Action Started");
            //individualPerform(leader, victim);
        //control.personPanel.fillPanel(victim);
        //control.gui.changeMiddlePanel(victim.getPlace().getHerePanel());
        //System.out.println("ActionRepeated!");

        }
    });



    actPanel.add(performButton2);

    return actPanel;
}


private void testButton()
{
    performButton.setText("Give this " + title + " to " + victim.getName());
    performButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            System.out.println("Action Started");
            //individualPerform(leader, victim);
        //control.personPanel.fillPanel(victim);
        //control.gui.changeMiddlePanel(victim.getPlace().getHerePanel());
        //System.out.println("ActionRepeated!");

        }
});
 }

private void setupSingleButton()
{
    performButton.removeAll();

    if (victim.getCurrentClub() == theSociety)
    {
    performButton.setText("Give this " + title + " to " + victim.getName());
    performButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent r) {

            oldPlace = victim.getPlace();
            individualPerform(leader, victim);
        control.personPanel.fillPanel(victim);

        System.out.println("ActionRepeated!");
        control.gui.changeMiddlePanel(oldPlace.getHerePanel());
        }
    });
}
}


private void individualPerform(Person thePerformer, Person theVictim)
{
    performer = thePerformer;
    victim = theVictim;

        victim.increaseLoyalty(1);

}

private void groupPerform(Person thePerformer, Vector<Person> theVictims)
{
    performer = thePerformer;
    victims = theVictims;

    for (int i=0; i<theVictims.size(); i++)
    {
        if (theVictims.get(i).getCurrentClub() == thePerformer.theSociety)
        {
            theVictims.get(i).increaseLoyalty(1);
        }
    }
}

 }
博安

每次调用testButton方法时,它将addActionListener再次调用,这将添加一个附加的侦听器。确保只添加一次侦听器。

如果您需要testButton重复调用,请将addActionListener调用移至其他方法,或者通过执行以下操作来测试是否已添加您的侦听器if (performButton.getActionListeners().length == 0) { ... }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么我的形象不断重复?

来自分类Dev

为什么我不断减少的数字计数重复?

来自分类Dev

为什么此代码不断循环?(ActionListener的概念)

来自分类Dev

为什么我的ActionListener无法使用?

来自分类Dev

Java做while循环不断重复,我不知道为什么

来自分类Dev

为什么在出现异常后我的代码会不断重复自身?

来自分类Dev

为什么我不断得到Null?

来自分类Dev

为什么我不断收到NoSuchElementException?

来自分类Dev

为什么我的命名管道不断被修改?

来自分类Dev

为什么新贵不断刷新我的流程?

来自分类Dev

为什么我的代码不断抛出KeyError?

来自分类Dev

为什么我不断收到预期声明

来自分类Dev

Android:为什么我的onResume()DialogInterface不断循环不断?

来自分类Dev

为什么我的ActionListener不适合我的按钮?

来自分类Dev

为什么我的输出重复?

来自分类Dev

为什么我的输出重复?

来自分类Dev

有人可以帮助我理解为什么此循环不断重复吗?

来自分类Dev

为什么“重复”属性不断返回 3 而不是 2?

来自分类Dev

为什么我的行号在Android Studio中不断消失?

来自分类Dev

为什么.lesshst不断出现在我的〜

来自分类Dev

为什么wiredep不断删除我的凉亭组件?

来自分类Dev

为什么我不断下载meteor-tool 1.3.1

来自分类Dev

Python while循环不断缩短我的清单!为什么?

来自分类Dev

为什么我不断收到禁止PayPal使用的403?

来自分类Dev

为什么git不断向我抛出“合并”警告消息?

来自分类Dev

为什么我的CPU的时钟速度不断下降?

来自分类Dev

MySQL不断重启(但我不确定为什么)

来自分类Dev

为什么我的程序不断发出GET请求?

来自分类Dev

为什么我不断收到此错误“错误:意外类型”

Related 相关文章

  1. 1

    为什么我的形象不断重复?

  2. 2

    为什么我不断减少的数字计数重复?

  3. 3

    为什么此代码不断循环?(ActionListener的概念)

  4. 4

    为什么我的ActionListener无法使用?

  5. 5

    Java做while循环不断重复,我不知道为什么

  6. 6

    为什么在出现异常后我的代码会不断重复自身?

  7. 7

    为什么我不断得到Null?

  8. 8

    为什么我不断收到NoSuchElementException?

  9. 9

    为什么我的命名管道不断被修改?

  10. 10

    为什么新贵不断刷新我的流程?

  11. 11

    为什么我的代码不断抛出KeyError?

  12. 12

    为什么我不断收到预期声明

  13. 13

    Android:为什么我的onResume()DialogInterface不断循环不断?

  14. 14

    为什么我的ActionListener不适合我的按钮?

  15. 15

    为什么我的输出重复?

  16. 16

    为什么我的输出重复?

  17. 17

    有人可以帮助我理解为什么此循环不断重复吗?

  18. 18

    为什么“重复”属性不断返回 3 而不是 2?

  19. 19

    为什么我的行号在Android Studio中不断消失?

  20. 20

    为什么.lesshst不断出现在我的〜

  21. 21

    为什么wiredep不断删除我的凉亭组件?

  22. 22

    为什么我不断下载meteor-tool 1.3.1

  23. 23

    Python while循环不断缩短我的清单!为什么?

  24. 24

    为什么我不断收到禁止PayPal使用的403?

  25. 25

    为什么git不断向我抛出“合并”警告消息?

  26. 26

    为什么我的CPU的时钟速度不断下降?

  27. 27

    MySQL不断重启(但我不确定为什么)

  28. 28

    为什么我的程序不断发出GET请求?

  29. 29

    为什么我不断收到此错误“错误:意外类型”

热门标签

归档