Java AWT Button
• Abstract Window Toolkit, also known as AWT is a library for
creating Graphical User Interfaces (GUIs) in Java application .
Button in Java AWT:
• The ‘Button’ class is a part of the ‘[Link]’ package which has a
collection of classes and methods for creating GUI components.
• Java AWT Buttons can be used to perform several actions like
saving a file, closing a window, submitting a form, or triggering any
specific action.
• When we press a button, AWT creates an instance of ActionEvent
and delivers it by calling processEvent on the button.
Syntax of Java AWT Button
Public class Button extends Component implements Accessible
Constructors of Button:
• There are 2 types of Constructors in the AWT Button class.
• Button(): Creates a new button with the label as an empty string.
• Button(String label): Creates a new button with the label as the
specified string as a parameter.
Inherited Methods:
The Methods included with AWT button are inherited by:
• [Link]
• [Link]
Examples of Java AWT Button
• Let us understand the AWT Button class and their methods with some examples given below:
Example 1 (Basic Button):
• The example given below is a simple implementation of a Button with a label.
import [Link];
import [Link];
Public class Main {
public static void main(String[] args) {
Frame frame = new Frame(“AWT Button Example”);
Button button = new Button(“Click Me!”);
[Link](150, 130, 100, 30);
[Link](button);
[Link](400, 300);
[Link](Null);
[Link](true);
}
}
Run the code using the following commands:
javac [Link]
java Main
Final Output:
Methods and its description:
• Void addActionListener(ActionListener l) - The action listener is
added to receive action events from this button.
• String getLabel() - Retrieves the label of this button.
• Protected String paramString() - Returns a string that represents
the status of this Button.
• Void setLabel(String label) - Sets the button’s label to be the
specified string.