java中JButton组件监听单击事件问题
package com.law;import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class JButtonTest extends JFrame
{
public JButtonTest()
{
setTitle("JButton组件");
Container container=getContentPane();
container.setLayout(new GridLayout(3,2,5,5));
JButton jb1=new JButton("按钮");
jb1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null,"弹出对话框");
}
});
container.add(jb1);
setBounds(100,100,300,300);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
new JButtonTest();
}
}
程序中监听按钮单击事件,老报如下错误,明明是用的匿名类啊,为什么报错?求解。谢谢
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (new ActionListener(){})
ActionListener cannot be resolved to a type