简体   繁体   中英

Using java.awt.* to create an eventlistener, why does it force you to use public abstract as the class?

I am reading about ActionListener . I have added the implements keyword and imported the java.awt.event.* library.

I followed the example and the oracle Api, but I get this error:

zx is not abstract and does not override abstract method window deactivated (java.awt.event.windowevent) in java.awt.event.windowlistener.

It is insisting that I make the class public abstract . Does anyone know why I have to do this? All the examples I have seen do not make their class abstract.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class zx extends JFrame implements WindowListener, ActionListener {

  public zx() {
    // create window
    super("title 2");
    setSize(100, 100);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    //container
    Container area = getContentPane();

    //layout manager
    GridBagLayout flowManager = new GridBagLayout();
    GridBagConstraints pos = new GridBagConstraints();
    area.setLayout(flowManager);

    //buttons
    JButton button1 = new JButton("1");
    pos.gridx = 20;
    pos.gridy = 11;
    area.add(button1, pos);

    JButton button2 = new JButton("2");
    pos.gridx = 15;
    pos.gridy = 11;
    area.add(button2, pos);

    JButton button3 = new JButton("3");
    pos.gridx = 4;
    pos.gridy = 4;
    area.add(button3, pos);

    JButton button4 = new JButton("4");
    pos.gridx = 2;
    pos.gridy = 2;
    area.add(button4, pos);

    JButton button5 = new JButton("5");
    pos.gridx = 0;
    pos.gridy = 0;
    area.add(button5, pos);
  }
}

all the examples do not set it to abstract.

Well then you shold do what the examples do, which is to actually implement the WindowListener and ActionListener interfaces.

Start with the simple example from How to Write an ActionListener found in the section from the Swing tutorial on Implementing Listeners . The try implementing a WindowListener. Then try a class that implements both and ActionListener and a WindowListener.

Also, class names should NOT start with a lower case character. "zx" does not follow conventions. The class name should also be more descriptive. You seem to have a problem following examples. I suggest you take more time to download working examples and then make changes to the working examples when you try to learn a new concept. That way your code will be better structured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM