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);
}
}
}
}
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] 삭제
몇 마디 만하겠습니다