In this article AWT Button Component we give the information about Buttons are used to execute tasks in an interface. For example, you can create a button that performs a particular action when the user clicks it. Clicking the button generates an event, which can be handled with an ActionListener.
AWT Button Component:
Button is a useful GUI (Graphical User Interface) component in AWT (Abstract Window Toolkit), which is used to create a clickable button in an interface. When a user clicks the button, an ActionEvent is generated, which we can handle.
Buttons are used to execute tasks in an interface. For example, you can create a button that performs a particular action when the user clicks it. Clicking the button generates an event, which can be handled with an ActionListener.
Some key properties of a Button:
Label: The text that appears on the button.
Enabled/Disabled: Whether the button is active or inactive.
Size: The width and height of the button.
ActionCommand: The command string used to identify the button.
AWT Button Attributes:
Button in AWT (Abstract Window Toolkit) has many attributes that we can use to control its appearance and behavior. Below are the main attributes of Button and their description:
1. Label
Description: The text that is displayed on the button is called label.
Methods:
getLabel(): Gets the label of the button.
setLabel(String label): Sets the label of the button.
Example:
Button b = new Button(“Click”);
b.setLabel(“Start”); // Change the label
2. Enabled/Disabled
Description: Determines whether the button is enabled or disabled. If the button is disabled, it cannot be clicked.
Methods:
setEnabled(boolean status): Enables or disables the button.
isEnabled(): Checks whether the button is active or not.
Example:
b.setEnabled(false); // Disable the button
3. ActionCommand
Description: A command string is set to identify the button. This is useful in event handling.
Methods:
getActionCommand(): Gets the action command of the button.
setActionCommand(String cmd): Sets an action command for the button.
Example:
b.setActionCommand(“start”); // Setting the action command
4. Size
Description: Controls the width and height of the button.
Methods:
setSize(int width, int height): Sets the size (width, height) of the button.
getSize(): Gets the current size of the button.
Example
b.setSize(100, 50); // Setting the button size to 100×50
5. Bounds
Description: Sets the position and size of the button within its container.
Methods:
setBounds(int x, int y, int width, int height): Sets the position (x, y) and size (width, height) of the button.
Example
b.setBounds(50, 100, 100, 50); // Setting the position and size of the button
6. Foreground Color (Text Color)
Description: Sets the color of the text displayed on the button.
Methods:
setForeground(Color c): Sets the color of the button’s text.
Example
b.setForeground(Color.RED); // Set the button’s text color to red
7. Background Color
Description: Sets the background color of the button.
Methods:
setBackground(Color c): Sets the background color of the button.
Example:
b.setBackground(Color.YELLOW); // Set the background color of the button to yellow
8. Font
Description: Sets the font (typeface, size and style) of the text displayed on the button.
Methods:
setFont(Font f): Sets the font of the button’s text.
Example
Font font = new Font(“Arial”, Font.BOLD, 14);
b.setFont(font); // Setting a bold font of size 14
9. Visible
Description: This property determines whether the button is visible on the screen or not.
Methods:
setVisible(boolean visible): Makes the button visible or invisible.
Example:
b.setVisible(false); // Makes the button invisible
// Button Example in AWT:
import java.awt.*;
import java.awt.event.*;
public class ButtonExample
{
public static void main(String[] args)
{
final Frame f = new Frame(“AWT Button Example”);
// Buttons Attributes
Button b = new Button(“Click mi”);
b.setBounds(100, 100, 100, 50);
b.setSize(200, 50); // Setting the button size to 200×50
b.setForeground(Color.RED); // Set the button’s text color to red
b.setBackground(Color.YELLOW); // Set the background color of the button to yellow
Font font = new Font(“Arial”, Font.BOLD, 14);
b.setFont(font); // Setting a bold font of size 14
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println(“Click on Button.”);
}
});
// Frame Attritutes
f.add(b);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.dispose();
}
});
}
}
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining