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();
}

}

你可能感兴趣的:(button)