I have a menu that I made, and the first time you select a program prom the menubar and click the launch button it launches the program, however when you select a different program, or to to the menu and reselect the same program then click launch it opens 2 of the programs selected, and if you do that again then 3, and so on. I am wondering what the problem is. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ISU implements ActionListener{
private JFrame mainFrame;
JLabel descriptionLabel;
public JLabel pictureLabel;
JButton launchApplication;
JPanel picPane;
public ISU (){
mainFrame = new JFrame ("Physics ISU Library");
JPanel mainPane = new JPanel ();
JPanel descriptPane = new JPanel ();
picPane = new JPanel ();
pictureLabel = new JLabel ();
JMenuBar menuBar = new JMenuBar ();
JMenu motionItem = new JMenu ("Motion");
JMenuItem gravitationalPE = new JMenuItem ("Gravitational Potential Energy"); gravitationalPE.setActionCommand ("GPE"); gravitationalPE.addActionListener (this);
JMenu forcesItem = new JMenu ("Forces");
JMenu energyItem = new JMenu ("Energy");
JMenu soundItem = new JMenu ("Sound");
JMenuItem transverseWave = new JMenuItem ("Transverse Wave"); transverseWave.setActionCommand("transverseWave");transverseWave.addActionListener(this);
JMenuItem dopplerEffect = new JMenuItem ("Doppler Effect"); dopplerEffect.setActionCommand("dopplerEffect"); dopplerEffect.addActionListener(this);
JMenu electricityItem = new JMenu ("Electricity");
JMenuItem resistorColorCodes = new JMenuItem ("Resistor Codes"); resistorColorCodes.setActionCommand("ResistorCC"); resistorColorCodes.addActionListener(this);
JMenu magnetismItem = new JMenu ("Magnetism");
motionItem.add(gravitationalPE);
soundItem.add(transverseWave); soundItem.add(dopplerEffect);
electricityItem.add(resistorColorCodes);
menuBar.add(motionItem); menuBar.add(forcesItem); menuBar.add(energyItem); menuBar.add(soundItem); menuBar.add(electricityItem); menuBar.add(magnetismItem);
mainFrame.add("North",menuBar);
descriptPane.setBackground (Color.yellow);
descriptionLabel = new JLabel ("This is the program that demonstrates how a blank works.");
launchApplication = new JButton ("Launch");
descriptPane.add(descriptionLabel);
descriptPane.add (launchApplication);
mainPane.setLayout(new BorderLayout ());
mainPane.add (descriptPane, BorderLayout.NORTH);
mainPane.add(picPane, BorderLayout.CENTER);
mainFrame.add (mainPane);
mainFrame.setSize (600,500);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ISU isu = new ISU ();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
MenuGraphics mg = new MenuGraphics ();
if ("transverseWave".equals(e.getActionCommand ())){
String program = e.getActionCommand();
picPane.add(mg);
mg.MenuGraphics(program);
launchApplication.setActionCommand("launchTransverseWave"); launchApplication.addActionListener(this);
descriptionLabel.setText("What this experiment is about.");
}
else if ("launchTransverseWave".equals(e.getActionCommand())){
TransverseWaveMethod ();
}
else if ("fanCarForces".equals(e.getActionCommand())){
}
else if ("ResistorCC".equals(e.getActionCommand ())){
launchApplication.setActionCommand("Launch ResistorCC");launchApplication.addActionListener(this);
descriptionLabel.setText ("In this program you can study the resistor colour codes.");
}
else if ("Launch ResistorCC".equals(e.getActionCommand())){
resistorProgram ();
System.out.println ("If Statement");
}
else if ("GPE".equals(e.getActionCommand())){
launchApplication.setActionCommand("LGPE");launchApplication.addActionListener(this);
descriptionLabel.setText ("<html>In this program you can explore how gravitational potential<br> energy changes as an object is moving.</html>");
}
else if ("LGPE".equals(e.getActionCommand())){
gravitationalPE ();
}
}
private void gravitationalPE() {
// TODO Auto-generated method stub
GravitationalPotentialEnergy gpe = new GravitationalPotentialEnergy ();
System.out.println ("Method accessed");
}
private void resistorProgram() {
// TODO Auto-generated method stub
ResistorColorCodeProgram rccp = new ResistorColorCodeProgram ();
System.out.println ("Method accessed");
}
public void TransverseWaveMethod() {
// TODO Auto-generated method stub
TransverseWaveProgram twp = new TransverseWaveProgram ();
}
}
Thanks for the help.