Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- Button:
- ~~~~~~~
- Button();
- Button(String);
- Methods:-
- String getLabel();
- void setActionCommand(String);
- String getActionCommand();
- void setLabel(String);
- public synchronized void addActionListener(ActionListener);
- */
- import java.awt.event.*;
- import java.awt.*;
- import java.applet.*;
- /*
- <applet CODE = "ButtonDemo" Height = 400 Width = 400>
- </applet>
- */
- public class ButtonDemo extends Applet implements ActionListener
- {
- Button b1, b2, b3;
- String msg = "";
- public void init()
- {
- b1 = new Button("One");
- b2 = new Button("Two");
- b3 = new Button("Three");
- add(b1);
- add(b2);
- add(b3);
- b1.addActionListener(this);
- b2.addActionListener(this);
- b3.addActionListener(this);
- }
- public void actionPerformed(ActionEvent be)
- {
- if(be.getActionCommand().equals(b1.getLabel()))
- {
- msg = "First";
- repaint();
- }
- if(be.getActionCommand().equals(b2.getLabel()))
- {
- msg = "Second";
- repaint();
- }
- if(be.getActionCommand().equals(b3.getLabel()))
- {
- msg = "Three";
- repaint();
- }
- }
- public void paint(Graphics g)
- {
- g.drawString(msg, 100, 100);
- }
- }

trfdtdr
Post new comment