Select不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent)

时间:2017-09-13 17:21:16

标签: java user-interface actionlistener

我已经尝试了所有可能性,但仍然出现错误:

  

select.java:4:错误:选择不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Select extends JFrame implements ActionListener
{
    JButton admin;
    JButton user;
JPanel panel;
public Select()
{
    admin=new JButton("admin");
    user=new JButton("user");
    panel=new JPanel(new GridLayout(3,1));
    panel.add(admin);
    panel.add(user);
    //admin.addActionListener();
    admin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent a) {
            Login page=new Login();
            page.setVisible(true);
        }
    });
    user.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent b) {
            Login page=new Login();
            page.setVisible(true);
        }
    });
}
}
   class selection
    {
        public static void main(String arg[])
         {
            try
            {
                Select frame=new Select();
                frame.setSize(300,200);
                frame.setVisible(true);
            }
            catch(Exception e)
            {
                JOptionPane.showMessageDialog(null, e.getMessage());
            }
        }   
    }

1 个答案:

答案 0 :(得分:1)

您正在使用匿名类作为useradmin的动作侦听器。您没有将Select用作动作监听器,因此只需从implements ActionListener声明中删除class Select

或者,将所需方法添加到Select

class Select extends JFrame implements ActionListener {
    ...

    public void actionPerformed(ActionEvent e) {
        ...
    }
}

但是,从您发布的代码中,Select无需实施ActionListener