Java JButton set action command text

Description

Java JButton set 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);/* w  w w  .j a  v a2s .  c  om*/
  }

  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