Java ActionEvent get action command text

Description

Java ActionEvent get action command text

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

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main extends JFrame {
  public Main() {
    super("JButton");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    JButton closeButton1 = new JButton("Close");
    closeButton1.setActionCommand("CloseDemo2s");
    closeButton1.addActionListener(new ButtonHandler());
    getContentPane().add(closeButton1);/*from   w  w  w.  j  ava 2s.  co  m*/
  }

  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}

class ButtonHandler implements ActionListener {
  // handle button event
  @Override
  public void actionPerformed(ActionEvent event) {
    System.out.println(event.getActionCommand());
    System.exit(0);
  }
}



PreviousNext

Related