import java.applet.Applet; import java.awt.Button; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /* <applet code="ButtonSetActionCommandExample" width=200 height=200> </applet> */ public class ButtonSetActionCommandExample extends Applet implements ActionListener{ String actionMessage=""; public void init(){ //create Button Button Button1 = new Button("I agree with the terms and conditions"); //add Button add(Button1); //set action listeners for buttons Button1.addActionListener(this); /* * By default, button's action command is it's label. But in * some cases, labels are too long and is not appropriate to use * it as an action command. In such situation you would want to * define custom short action command for a button. * * To set custom action command for a button, use * void setActionCommand(String command) * method of AWT Button class. */ Button1.setActionCommand("Agree"); } public void paint(Graphics g){ g.drawString(actionMessage,10,50); } public void actionPerformed(ActionEvent ae){ /* * Get the action command using * String getActionCommand() method. */ String action = ae.getActionCommand(); actionMessage = action + " button pressed!"; repaint(); } }
Java Program to Set Action Command For AWT Button.
Reviewed by
on
December 21, 2019
Rating:
No comments: