执行Java操作getsource()

时间:2018-04-18 14:37:11

标签: java actionlistener

我有一个java代码,其中包含3个动作侦听器,对应于我的java页面中的菜单。当我点击菜单时,我想要一个特定的页面出现。

这是我的代码(当我点击它不起作用的菜单时)

JPanel panContents = new JPanel();
PanCommander panCommander = new PanCommander();
PanHistorique panHistorique = new PanHistorique();
PanModifierProfil panModifierProfil = new PanModifierProfil();

public class PanCommander extends JPanel
{
    public void initialisation () {
       this.setBackground(Color.YELLOW);
    }
}
//The other 2 pans are 2 other colors, when i click on a menu i want the page to change the color

//In the constructor :
    panContents.add(panCommander, COMMANDER); 
    panContents.add(panHistorique, HISTORIQUE);
    panContents.add(panModifierProfil, MODIFIER_PROFIL);

    //We say to our JFrame that our JPanel will be its content pane
    this.getContentPane().add(panContents); 
    //We initialise the menu
    this.initialisationMenu();

    //its code :
    private void initialisationMenu () {
      JMenuItem commander = new JMenuItem("Commander");
      commander.addActionListener(this);
      JMenuItem modifierProfil = new JMenuItem("Modifier Profil");
      modifierProfil.addActionListener(this);
      JMenuItem historique = new JMenuItem("Historique");
      historique.addActionListener(this);

      JMenu menuMonCompte = new JMenu("Mon compte");
      menuMonCompte.add(commander);
      menuMonCompte.add(modifierProfil);
      menuMonCompte.add(historique);

      JMenu menuDeconnexion = new JMenu("Deconnexion");

      barreMenu.add(menuMonCompte);
      barreMenu.add(menuDeconnexion);
}
//And finally my actionPerformed ( that doesn't work )
public void actionPerformed (ActionEvent event) {
    if (event.getSource() == panCommander) {
        cartes.show(panContents, COMMANDER);
        panelVisible(COMMANDER); 
    }
    if (event.getSource() == panHistorique) {
        cartes.show(panContents, HISTORIQUE);
        panelVisible(HISTORIQUE);
    }
    if (event.getSource() == panModifierProfil) {
        cartes.show(panContents, MODIFIER_PROFIL);
        panelVisible(MODIFIER_PROFIL);
    }
} 

如果有人可以帮助我,那将是非常好的!

0 个答案:

没有答案