3

这是我的代码:

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


public class FirstGui extends JFrame {
    private JLabel label;
    private JButton button;

    public FirstGui() {

        setLayout(new FlowLayout());

        button = new JButton("Click for sex");
        add(button);

        label = new JLabel("");
        add(label);

        event e = new event();
        button.addActionListener(e);
    }

    public class event implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            label.setText("how you can see wors here");

        }
    }

    public static void main(String [] args) {
        FirstGui gui = new FirstGui();

        gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
        gui.setSize(200, 200);
        gui.setTitle("Title");
        gui.setVisible(true);

    }   

}

它会产生一个错误:

ActionEvent 无法解析为 FirstGui.java /Test/src 第 26 行 Java 问题

ActionListener 无法解析为 FirstGui.java /Test/src 第 24 行 Java 问题

AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于参数 (FirstGui.event) FirstGui.java /Test/src 第 21 行 Java 问题

有什么问题???我是 Java 新手。

4

3 回答 3

5

导入以下内容:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
于 2013-01-24T20:40:39.363 回答
0

ActionEventActionListener位于java.awt.event包装内。

进口java.awt.*是不够的。

于 2013-01-24T20:39:52.160 回答
0

Both of these classes require you to import them. You can do so by importing everything in java.awt.event:

import java.awt.event.*;

or you may just want to import specifically what you're using:

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

Remember that it's considered good practice to import individual classes (the latter option) instead of importing whole packages.

If you ever get stuck like this again, looking at The Docs for any Java Class will tell you exactly what you need to import with a little diagram that looks like this:

java.lang.Object
   java.util.EventObject
      java.awt.AWTEvent
          java.awt.event.ActionEvent
于 2013-01-24T20:42:26.293 回答