为什么我的ActionListener会不断重复?

时间:2014-08-24 14:45:14

标签: java actionlistener repeat

我制作了一个JButton并为它添加了一个“AddActionListener”,但它不断重复相同的动作几次(每次按下按钮时重复2-6次)。

我确信我最终会找到它,但你能在这段代码中看到任何可以说明它为什么重复的内容吗?

它调用的任何方法都不应该重复该操作。此外,我可以从其他地方调用这些方法,这没关系。

EDITED:以下是问题类的完整代码:

以下是问题类导入的完整代码

 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);
        }
    }
}

 }

1 个答案:

答案 0 :(得分:0)

每次调用testButton方法时,它都会再次调用addActionListener,这会添加一个额外的侦听器。确保您只添加一次监听器。

如果您需要反复拨打testButton,请将addActionListener来电转移到其他方法,或者通过if (performButton.getActionListeners().length == 0) { ... }

测试您的听众是否已添加