简体   繁体   中英

adding ActionListener to JMenuItem not working

I am trying to make a drop down menu for this program which displays a bunch of diseases and i want it so when I click on one of the items from the JMenuBar, it draws out a picture by calling paintComponent, but my actionlisteners dont work in the first place to even see if the JMenuItem is clicked. Where is my error at? I have tried everything I could

I have tried placing the actionlisteners right after i declare the jmenuitem objects with no success in doing so, tried meddling with my actionPerformed method with little success in my problem being fixed.

public void makeDDM()
{
    ddmFRAME = new JFrame ("you.");
    ddmPANEL = new JPanel();
    ddmFRAME.setSize(300, 700);             
    ddmFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    ddmFRAME.setLocation(0,0);
    ddmFRAME.setResizable(true);
    DDM dmeOne = new DDM();
    ddmIMAGE dmeTwo = new ddmIMAGE();
    ddmTITLE dmeThree = new ddmTITLE();
    ddmPANEL.setLayout(new GridLayout(3,1));
    ddmPANEL.add(dmeThree); //yellow
    ddmPANEL.add(dmeOne); //blue
    ddmPANEL.add(dmeTwo); //red
    ddmFRAME.add(ddmPANEL);
    ddmFRAME.setVisible(true);


}

class DDM extends JPanel implements ActionListener
{
    public DDM()
    {                        
        setBackground(Color.ORANGE);
        jmb = new JMenuBar();
        ddmFRAME.setJMenuBar(jmb);
        JMenu file = new JMenu("Examples Of Diseases / Conditions");
        jmb.add(file);
        file.setOpaque(true);
        file.setBackground(Color.GREEN.darker());
        JMenuItem itemOne = new JMenuItem("Anorexia");
        file.add(itemOne);
        itemOne.addActionListener(this);
        JMenuItem itemTwo = new JMenuItem("Cancer");
        file.add(itemTwo);
                    itemTwo.addActionListener(this);
        JMenuItem itemThree = new JMenuItem("High Blood Cholesetrol");
        file.add(itemThree);
                    itemThree.addActionListener(this);
                    JMenuItem itemFour = new JMenuItem("Heart Disease");
        file.add(itemFour);
                    itemFour.addActionListener(this);
        JMenuItem itemFive = new JMenuItem("Obesity");
        file.add(itemFive);
                    itemFive.addActionListener(this);
        JMenuItem itemSix = new JMenuItem("Stroke");
        file.add(itemSix); 
                    itemSix.addActionListener(this);
        itemOne.setOpaque(true);
        itemOne.setBackground(Color.GREEN.darker());
        itemTwo.setOpaque(true);
        itemTwo.setBackground(Color.GREEN.darker());
        itemThree.setOpaque(true);
        itemThree.setBackground(Color.GREEN.darker());
        itemFour.setOpaque(true);
        itemFour.setBackground(Color.GREEN.darker());
        itemFive.setOpaque(true);
        itemFive.setBackground(Color.GREEN.darker());
        itemSix.setOpaque(true);
        itemSix.setBackground(Color.GREEN.darker());
                    add(jmb, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent evt) 
    {
                if(evt.getSource() == itemOne)
                {
                    anorexia = true;
                    System.out.println(anorexia + " hi nigd");
                    dmeTwo.repaint();
                }
                else if(evt.getSource() == itemTwo)
                {
                    cancer = true;
                    dmeTwo.repaint();
                }
                else if(evt.getSource() == itemThree)
                {
                    heart1 = true;
                    dmeTwo.repaint();
                }
                else if(evt.getSource() == itemFour)
                {
                    heart2 = true;
                    dmeTwo.repaint();
                }
                else if(evt.getSource() == itemFive)
                {
                    obesity = true;
                    dmeTwo.repaint();
                }
                else if(evt.getSource() == itemSix)
                {
                    stroke = true;
                    dmeTwo.repaint();
                }
            }
}

// i declared image file before this paintComponent method

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        System.out.println(anorexia);

        if(anorexia)
        {
        System.out.println("hi");
            g.drawImage(ddmPIC1, 0, 0, this);
        }
        else if(cancer) 
        {
            g.drawImage(ddmPIC2, 0, 0, this);               
        }   
        else if(heart1)
        {
            g.drawImage(ddmPIC3, 0, 0, this);
        }
        else if(heart2)
        {
            g.drawImage(ddmPIC4, 0, 0, this);
        }
        else if(obesity)
        {
            g.drawImage(ddmPIC5, 0, 0, this);
        }
        else if(stroke)
        {
            g.drawImage(ddmPIC6, 0, 0, this);
        }

    }
}

I expect the boolean declared to become true when I click one of the JMenuItems , but after testing, this doesnt seem to be working at all. Any help is appreciated. Thank you

You are re-declaring the menu items in the method where you create them, making them local to that method and hiding the instance variable of the same name. Thus in the action listener, you are comparing the source to a value of null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM