Class Button in AWT in Java

0
192
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class Button in Java:

This class Button is responsible for creating a labeled or unlabeled button in the GUI. We need to use setActionCommand() that specify a String to identify which button has generated the ActionEvent.

The Button is the familiar labeled button object. It inherits most of its functionality from Component. For example, to change the font of the Button, you would use the Component’s setFont() method. The Button sends java.awt.event.ActionEvent objects to its listeners when it is pressed.

The structure of the class Button is given as :

public class java.awt.Button extends java.awt.Component{
//constructors
public Button();//creates an button without a label in it
public Button(String Label);//creates a button with the label -Label
//methods:
public void setLabel(String label);//sets a label for the button.
public void addNotify();
public String getLabel();
public synchronized void setLabel(String label);
public synchronized void addActionListener(ActionListener l);
public synchronized void removeActionListener(ActionListener l);
public String getActionCommand();
public void setActionCommand(String command);
protected String paramString();
protected void processActionEvent(ActionEvent e);
protected void processEvent(AWTEvent e);
} 

The details of the class structure are given as follows:

public Button();

public Button() constructs a Button object with no label.

public Button (String label);

public Button (String label) constructs a Button object with text of label.

Parameter
label – The text for the label on the button

public void addActionListener (ActionListener l);

public void addActionListener (ActionListener l) method add a listener for the action event.

Parameter
l – An object that implements the ActionListener interface.

public void addNotify();

public void addNotify() method creates Button’s peer.

public String getActionCommand();

public String getActionCommand() method returns the string used for the action command.

This method returns the current action command string.

public String getLabel();

public String getLabel() method returns text of the Button’s label.

This method returns the text of the Button’s label.

public void removeActionListener (ActionListener l);

public void removeActionListener (ActionListener l) method remove an action event listener.

Parameter
l – One of this Button’s ActionListeners.

public void setActionCommand (String command);

public void setActionCommand (String command) method specify the string used for the action command.

Parameter
command – New action command string.

public synchronized void setLabel (String label);

public synchronized void setLabel (String label) method changes the Button’s label to label.

Parameter
label – New text for label of Button.

protected String paramString();

protected String paramString() method is a helper method for toString() used to generate a string of current settings.

This method returns a String with the current settings of Button.

protected void processActionEvent (ActionEvent e);

protected void processActionEvent (ActionEvent e) method receives the Action events for processing. Normally, this method is called by processEvent().

Parameter
e – The action event to process.

protected void processEvent (AWTEvent e);

protected void processEvent (AWTEvent e) method receives a low level AWTEvents for processing.

Parameter
e – The event to process.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here