2

因此,我正在制作此代码以根据用户点击写入文件。我唯一的问题是我在“public class prog”上遇到错误。prog 名称是我得到错误的地方:它说:类型 prog 必须实现继承的抽象方法 ActionListener.actionPerformed(ActionEvent)。当我执行添加未继承方法的快速修复时,它会将动作侦听器方法添加到我的代码末尾,但其中没有任何内容。如果程序中已经有动作监听器,为什么它告诉我需要实现它们?为什么当我在最后添加它时,即使里面什么都没有,它也能正常工作?

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.*;

public class prog extends JFrame implements ActionListener {

//create newLine
final String newLine = System.getProperty("line.separator");

//create buttons
JPanel row1 = new JPanel();
JButton oneLeft = new JButton("oneLeft");
JButton oneRight = new JButton("oneRight");

JPanel row2 = new JPanel();
JButton twoLeft = new JButton("twoLeft");
JButton twoRight = new JButton("twoRight");

JPanel row3 = new JPanel();
JButton threeLeft = new JButton("threeLeft");
JButton threeRight = new JButton("threeRight");


public prog() {
    super("Prog");
    setLookAndFeel();
    setSize(400, 800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout layout = new GridLayout(3, 2);
    setLayout(layout);

    //create outStream for writing to file
    try {
        final File numClicks = new File("numClicks.properties");
        final FileOutputStream outStream = new FileOutputStream(numClicks);
        //add Listeners
        oneLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "oneLeft has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        oneRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "oneRight has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        twoLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "twoLeft has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        twoRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "twoRight has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        threeLeft.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "threeLeft has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });

        threeRight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    write(outStream, "threeRight has been clicked.");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
    } catch (IOException ioe) {
        System.out.println("The file could not be written.");
    }




    row1.add(oneLeft);
    row1.add(oneRight);
    row2.add(twoLeft);
    row2.add(twoRight);
    row3.add(threeLeft);
    row3.add(threeRight);
    add(row1);
    add(row2);
    add(row3);



    setVisible(true);
}

private void setLookAndFeel() {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (Exception e) {
        //ignore error
    }
}

void write(FileOutputStream stream, String output) throws IOException {
    output = output + newLine;
    byte[] data = output.getBytes();
    stream.write(data, 0, data.length);
}

public static void main(String[] args) {
    prog progApp = new prog();
}
}
4

2 回答 2

3

你的班级不应该实现ActionListener. 不是编写实现接口的顶级类,而是编写一堆小的内联类(称为匿名内部类),当您说new ActionListener().

于 2013-08-14T22:18:52.143 回答
2

implement ActionListener它,但你实际上并没有实现所需的方法(即,actionPerformed())。因此,您的类对编译器无效。

你需要这样的方法:

@Override
public void actionPerformed(ActionEvent e) {
    // ...
}

接口的工作方式是它定义了implements它必须......好吧......实现的类。这样,任何其他进程都可以将其视为一个ActionListener并知道某些方法已被定义。

Java 试图让多态成为你朋友的另一种方式。

为了从下面的评论中解决问题,实际上看到一个类实现一个接口(如KeyListener)并在不使用它的情况下定义该方法并不少见。

例如,KeyListener要求您实现三种不同的方法:

  • public void keyPressed(KeyEvent e);
  • public void keyReleased(KeyEvent e);
  • public void keyTyped(KeyEvent e);

假设我只真正关心keyPressed. 那么我的班级可能看起来像这样:

public class MyKeyListener implements KeyListener {

    @Override
    public void keyPressed(KeyEvent e) {
        // do stuff
    }

    @Override
    public void keyReleased(KeyEvent e){}

    @Override
    public void keyTyped(KeyEvent e){}

}
于 2013-08-14T22:13:18.063 回答