错误:LoginController不是抽象的,并且没有重写抽象方法actionPerformed

huangapple go评论63阅读模式
英文:

Error:LoginController is not abstract and does not override abstract method actionPerformed

问题

错误:错误:(4, 8) java: LoginController 不是抽象的,并且没有重写 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent)

我得到了这个错误,但我不确定原因。我有两个类 View 和 Controller。Controller 为 View 内的一个按钮实现了 ActionListner。当用户按下按钮时,它应该运行 actionPerformed 方法。

LoginView 类

import javafx.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class LoginView extends JFrame {

    private Container container;
    private JPanel panel;
    private JButton loginButton;
    private JLabel userLabel, passwordLabel;
    private JTextField usernameField;
    private JPasswordField passwordField;

    public LoginView(){

        setTitle("Login");
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel = new JPanel();
        panel.setBackground(Color.GREEN);
        add(panel);
        panel.setLayout(null);

        userLabel = new JLabel("Username:");
        userLabel.setBounds(10,20,80,25);
        userLabel.setFont(new Font("Serif", Font.PLAIN, 18));

        usernameField = new JTextField();
        usernameField.setBounds(10,40,270,40);

        passwordLabel = new JLabel("Password:");
        passwordLabel.setBounds(10,100,80,25);
        passwordLabel.setFont(new Font("Serif", Font.PLAIN, 18));

        passwordField = new JPasswordField();
        passwordField.setBounds(10,120,270,40);

        loginButton = new JButton("Login");
        loginButton.setBounds(70,190,150,60);
        loginButton.addActionListener(new LoginController());

        panel.add(userLabel);
        panel.add(usernameField);
        panel.add(passwordLabel);
        panel.add(passwordField);
        panel.add(loginButton);

        container = getContentPane();
        setVisible(true);
    }
}

LoginController 类

import javafx.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginController implements ActionListener{

    public void actionPerformed(ActionEvent e){

    }
}
英文:

Error: Error:(4, 8) java: LoginController is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener

I am getting this error and I am not sure why. I have two classes View and Controller. The Controller implements ActionListner for a button that is inside the View. When a user presses a button it should run the actionPerformed method.

LoginView class

 import javafx.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class LoginView extends JFrame {
private Container container;
private JPanel panel;
private JButton loginButton;
private JLabel userLabel, passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
public LoginView(){
setTitle("Login");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setBackground(Color.GREEN);
add(panel);
panel.setLayout(null);
userLabel = new JLabel("Username:");
userLabel.setBounds(10,20,80,25);
userLabel.setFont(new Font("Serif", Font.PLAIN, 18));
usernameField = new JTextField();
usernameField.setBounds(10,40,270,40);
passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(10,100,80,25);
passwordLabel.setFont(new Font("Serif", Font.PLAIN, 18));
passwordField = new JPasswordField();
passwordField.setBounds(10,120,270,40);
loginButton = new JButton("Login");
loginButton.setBounds(70,190,150,60);
loginButton.addActionListener(new LoginController());
panel.add(userLabel);
panel.add(usernameField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(loginButton);
container = getContentPane();
setVisible(true);
}
}

LoginController class

import javafx.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginController implements ActionListener{
public void actionPerformed(ActionEvent e){
}
}

答案1

得分: 1

你导入了错误的 ActionEvent 类,应该导入以下类:

import java.awt.event.ActionEvent
英文:

You imported the wrong ActionEvent class, this is the one to import :

import java.awt.event.ActionEvent

huangapple
  • 本文由 发表于 2020年10月24日 00:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64503775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定