How to make a second action listener into an object?

Deni :

Currently, I'm trying to create a notepad that has a menu bar as well as a pop-up menu. The problem is that I cannot have 2 action listeners in the constructor. I know that I have to create the second action listener in an object, however, I am unsure how. I marked the places in which I encounter the error. Thank you in advance!

The error message that I am getting is as follows: incompatible types: cannot be converted to Action Listener - Leaking this in the constructor

public Notepad1)
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    String Mlabs[] = { "File", "Edit", "Format", "View" };
    String Elabs0[] = { "New", "Open", "Save", "Save as...", "Exit" };
    String Elabs1[] = { "Select", "Copy", "Paste"};
    String Elabs2[] = { "Word", "Zoom"};
    String Elabs3[] = { "View Help", "About Notepad"};

    JMenuBar jmb = new JMenuBar();  

    JMenu jm1 = new JMenu(Mlabs[0]);         
        jm1.setMnemonic(Mlabs[0].charAt(0));     
        for (int j=0; j<Elabs0.length; j++)
        {
            JMenuItem jmi = new JMenuItem(Elabs0[j]);   
            jm1.add(jmi);   

        }
        jmb.add(jm1);

        JMenu jm2 = new JMenu(Mlabs[1]);         
        jm2.setMnemonic(Mlabs[1].charAt(1));     
        for (int j=0; j<Elabs1.length; j++)
        {
            JMenuItem jmi = new JMenuItem(Elabs1[j]);   
            jm2.add(jmi);   
            jmi.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    JComponent jc = (JComponent)ae.getSource();
                    if(ae.getActionCommand().equals("Select"))
                    {
                        jta.selectAll();
                    }
                    if(ae.getActionCommand().equals("Copy"))
                    {
                        copied = jta.getSelectedText();
                    }
                    if(ae.getActionCommand().equals("Paste"))
                    {
                        jta.setText(jta.getText() + copied);
                    }
                }
            });
        }
        jmb.add(jm2);

        JMenu jm3 = new JMenu(Mlabs[2]);         
        jm3.setMnemonic(Mlabs[2].charAt(2));     
        for (int j=0; j<Elabs2.length; j++)
        {
            JMenuItem jmi = new JMenuItem(Elabs2[j]);   
            jm3.add(jmi);                       
        }
        jm3.addSeparator();
        jmb.add(jm3);

        JMenu jm4 = new JMenu(Mlabs[3]);         
        jm4.setMnemonic(Mlabs[3].charAt(3));     
        for (int j=0; j<Elabs3.length; j++)
        {
            JMenuItem jmi = new JMenuItem(Elabs3[j]);   
            jm4.add(jmi);                       
        }
        jmb.add(jm4);            

    frame.setJMenuBar(jmb);                     

    JComponent comp = (JComponent)frame.getContentPane();
    comp.setLayout(new GridLayout(1, 1));
    jta.setFont(new Font("TimesRoman", Font.BOLD, 18));
    comp.add(jta);

    jpm = new JPopupMenu();
    String miLab[] = { "Select", "Copy", "Paste" };
    for (int i=0; i<miLab.length; i++)
    {
        JMenuItem jmi = new JMenuItem(miLab[i]);
        jmi.setActionCommand(miLab[i]);
        jmi.addActionListener(this); // <-- ERROR 1
        jpm.add(jmi);
    }

    JMenu newElem = new JMenu("Edit");
    for (int i=0; i<miLab.length; i++) 
    {
        JMenuItem item = new JMenuItem("rozwijka "+miLab[i]);
        newElem.add(item);
        item.addActionListener(this); // <-- ERROR 2
    }
    jpm.add(newElem);

    frame.setSize(1000,500);
    frame.setVisible(true);       
}


MouseAdapter ma = new MouseAdapter()
{
    public void mouseReleased(MouseEvent me)
    {
        if(me.isPopupTrigger())
        {       
            compMenu = me.getComponent();
            jpm.show( compMenu, me.getX(), me.getY() );
        }
    }
};
ControlAltDel :
    item.addActionListener(this); // <-- ERROR 2

Just do this like you did the other one:

    item.addActionListener(new Actionlistener() {
      public void actionPerformed(ActionEvent ae) {
        //...;
      }
    });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=299784&siteId=1