Why does my ActionListener keep repeating?

Matt Payne

I made a JButton and added an "AddActionListener" to it, but it keeps repeating the same action several times (2-6 times each time I press the button).

I'm sure I'll find it eventually, but can you see anything in this code that would show why it's repeating?

It shouldn't be possible for any of the methods it calls to make the action repeat. Also, I can call those methods from other places, and it's okay.

EDITED: Here is the whole code for the problem class:

Here is the whole code for the problem-class import

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

 }
Boann

Each time you call your testButton method it will call addActionListener again, which will add an additional listener. Make sure you're only adding the listener once.

If you need to call testButton repeatedly, then move the addActionListener call to a different method, or test if your listener has been added already by doing if (performButton.getActionListeners().length == 0) { ... }

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does Ubuntu keep asking for my gmail password?

분류에서Dev

Why does my display keep turning off every 10 minutes?

분류에서Dev

Why does my "Quiz" program keep making two options correct answers?

분류에서Dev

How do i keep my loop from repeating my random values?

분류에서Dev

Why deos my "While" loop keep going on?

분류에서Dev

Why are the name repeating in my array, and how would i get a txt file to sort

분류에서Dev

Why does light-locker keep the screen black after unlock?

분류에서Dev

Why does this canvas keep growing? (dynamic resizing of canvas)

분류에서Dev

Why does apt-get keep waiting for headers on Oracle repository?

분류에서Dev

Angular: Why does my formArray not validate or update?

분류에서Dev

Why does Doctrine QueryBuilder destroy my query?

분류에서Dev

Why does Outlook 2013 not send my email?

분류에서Dev

Why does my function print '0'?

분류에서Dev

Why does my linq to sql query fail?

분류에서Dev

Why does my JavaScript instances return the same?

분류에서Dev

Why does my blob not get sent?

분류에서Dev

Why does my bubble sort method not work?

분류에서Dev

Why does my wifi fail to stay connected?

분류에서Dev

Why does my wifi Internet intermittently disappear?

분류에서Dev

Why my cron Job does not work?

분류에서Dev

Why does my xdotool key command not work?

분류에서Dev

Why does my handson table duplicate itself?

분류에서Dev

Why does my bash script hang?

분류에서Dev

Why does itunecconnect not accept my geoJSON file?

분류에서Dev

Why does my condition always evaluate as true?

분류에서Dev

Why does my cronjob not execute my shell-script?

분류에서Dev

why does my python code not encrypt or decrypt my message

분류에서Dev

Why does my global menu freeze in Ubuntu 13.10?

분류에서Dev

Why Does AdMob Free Crash My App in the iOS Simulator?

Related 관련 기사

뜨겁다태그

보관