0% found this document useful (0 votes)
2 views

updated button1

The document provides an overview of the Java AWT Button class, which is part of the Abstract Window Toolkit for creating GUIs in Java. It describes the constructors, methods, and an example implementation of a button that can trigger actions. Key methods include adding action listeners and retrieving or setting the button's label.

Uploaded by

harlearncs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

updated button1

The document provides an overview of the Java AWT Button class, which is part of the Abstract Window Toolkit for creating GUIs in Java. It describes the constructors, methods, and an example implementation of a button that can trigger actions. Key methods include adding action listeners and retrieving or setting the button's label.

Uploaded by

harlearncs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 ‘java.awt’ 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:
• java.awt.Component
• java.lang.Object
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 java.awt.Button;
import java.awt.Frame;
Public class Main {
public static void main(String[] args) {
Frame frame = new Frame(“AWT Button Example”);
Button button = new Button(“Click Me!”);
button.setBounds(150, 130, 100, 30);
frame.add(button);
frame.setSize(400, 300);
frame.setLayout(Null);
frame.setVisible(true);
}
}
Run the code using the following commands:
javac Main.java
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.

You might also like