这段代码有什么问题?:
当我声明 私有(private)类监听器实现 ActionListener IDE 仅将 Listener 标记为红色,并表示它不是抽象的
package hello;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class hello{
private JFrame mainFrame;
private JLabel title;
private JPanel mainPanel;
public hello(){
prepareGUI();
}
public static void main(String[] args){
hello helloo = new hello();
helloo.Event();
}
private void prepareGUI(){
mainFrame = new JFrame("This is a test project");
mainFrame.setSize(500, 500);
mainFrame.setLayout(new GridLayout(3,1));
mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout());
title = new JLabel("",JLabel.CENTER);
mainFrame.add(mainPanel);
mainFrame.add(title);
mainFrame.setVisible(true);
}
private void Event(){
JButton button1 = new JButton("Test");
button1.setSize(15,10);
button1.setActionCommand("Test");
button1.addActionListener(new Listener());
mainPanel.add(button1);
mainFrame.setVisible(true);
}
private class Listener implements ActionListener{
public void action(ActionEvent e){
String com = e.getActionCommand();
if(com.equals("Test")){
title.setText("button clicked");
}
}
}
}
我是 JAVA 的新手,昨天开始,所以任何建议都会有所帮助
附言我正在使用 NetBeans IDE 7.4
最佳答案
您还没有实现 ActionListener
所需的方法界面。
在那个类(class)的某个地方,填写:
@Override
public void actionPerformed(ActionEvent e){
// Fill
}
您有一个
action(ActionEvent)
方法,但拼写错误。
关于java - IDE 将 Listener 标记为红色并表示它不是抽象的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916867/