- /*
- Set Action Command For AWT Button Example
- This java example shows how to set custom action command for AWT button using
- setActionCommand method of Java AWT Button class.
- */
- 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();
- }
- }
Welcome to the world of Java examples, organized by categories and Java packages. Java examples (Java sample source code) help to understand functionality of various Java classes and methods as well as various programming techniques in a simple way, which is otherwise very hard to learn by reading tutorials or Java API. So start exploring...
Sunday, November 27, 2011
Set Action Command For AWT Button Example
Subscribe to:
Post Comments (Atom)
How to find the most appropriate Keywords?
🔍 Step 1: Understand Your Business and Audience Define your products, services, or content . Identify your target audien...
-
/* Pause Thread Using Sleep Method Example This Java example shows how to pause currently running thread using sleep method of Jav...
-
/* Java String split example. This Java String split example describes how Java String is split into multiple Java String objects. */ ...
-
// File Name GreetingClient.java import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...
No comments:
Post a Comment