I .
Java Program to create a list box
import java.awt.*;
import java.awt.event.*;
public class SimpleListBoxAWT {
public static void main(String[] args) {
// Create frame
Frame frame = new Frame("Simple List Box");
frame.setSize(200, 200);
frame.setLayout(new FlowLayout());
// Create a list box
String[] items = {"Item 1", "Item 2", "Item 3", "Item 4"};
List listBox = new List();
for (String item : items) {
listBox.add(item);
}
// Add list box to frame
frame.add(listBox);
// Add window closing event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
// Set frame visibility
frame.setVisible(true);
}}
II.Java program with list box
import java.awt.*;
import java.awt.event.*;
public class SimpleTextBoxAWT {
public static void main(String[] args) {
// Create frame
Frame frame = new Frame("Simple Text Box");
frame.setSize(300, 150);
frame.setLayout(new FlowLayout());
// Create a text box
TextField textBox = new TextField(20);
// Add text box to frame
frame.add(textBox);
// Add window closing event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
// Set frame visibility
frame.setVisible(true);
}
}
II .Keyboard and Mouse Events in Java
In Java AWT, event handling is done using event listeners. The primary event types for
keyboard and mouse interactions are:
1. Keyboard Events (KeyListener)
• Used to capture keyboard inputs such as key presses, key releases, and key typing.
• Implemented using the KeyListener interface.
KeyListener Methods:
• keyPressed(KeyEvent e): Called when a key is pressed.
• keyReleased(KeyEvent e): Called when a key is released.
• keyTyped(KeyEvent e): Called when a character key is typed.
Example Usage:
java
CopyEdit
textBox.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
System.out.println("Key Released: " + e.getKeyChar());
}
public void keyTyped(KeyEvent e) {
System.out.println("Key Typed: " + e.getKeyChar());
}
});
2. Mouse Events (MouseListener and MouseMotionListener)
• Used to detect mouse actions such as clicks, movements, and drags.
MouseListener Methods (Click & Press Events):
• mouseClicked(MouseEvent e): Called when the mouse is clicked.
• mousePressed(MouseEvent e): Called when the mouse button is pressed.
• mouseReleased(MouseEvent e): Called when the mouse button is released.
• mouseEntered(MouseEvent e): Called when the mouse enters a component.
• mouseExited(MouseEvent e): Called when the mouse exits a component.
Example Usage:
java
CopyEdit
frame.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at: " + e.getX() + ", " + e.getY());
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
});
MouseMotionListener Methods (Drag & Move Events):
• mouseMoved(MouseEvent e): Called when the mouse is moved.
• mouseDragged(MouseEvent e): Called when the mouse is dragged.
Example Usage:
java
CopyEdit
frame.addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse Moved to: " + e.getX() + ", " + e.getY());
}
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged to: " + e.getX() + ", " + e.getY());
}
});
These event listeners can be added to various AWT components like Frame, Button,
TextField, etc. Let me know if you need any specific implementation!
IV.Button in Java (AWT)
A button in Java AWT is a GUI component that can be clicked to trigger an action. It is
represented by the Button class in the java.awt package.
Key Features of AWT Button:
1. Constructor:
o Button(String label): Creates a button with the specified label.
2. Event Handling:
o ActionListener is used to detect button clicks.
o The method actionPerformed(ActionEvent e) is implemented to define the
action.
Example Program: Simple AWT Button
Below is a program that creates a simple button and displays a message when it is clicked.
java
CopyEdit
import java.awt.*;
import java.awt.event.*;
public class SimpleButtonAWT {
public static void main(String[] args) {
// Create frame
Frame frame = new Frame("AWT Button Example");
frame.setSize(300, 150);
frame.setLayout(new FlowLayout());
// Create button
Button button = new Button("Click Me");
// Create label to display message
Label label = new Label("Button not clicked yet");
// Add action listener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});
// Add components to frame
frame.add(button);
frame.add(label);
// Add window closing event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
// Set frame visibility
frame.setVisible(true);
}
}
Explanation of Code:
• A Frame is created as the main window.
• A Button is added with the label "Click Me".
• A Label is used to display messages.
• An ActionListener is attached to detect button clicks and update the label text.
• A WindowListener is used to close the window properly.
V.Basic Concepts of CheckBox in Java AWT
A CheckBox in Java AWT is a toggleable UI component that allows users to select or deselect
an option. It is represented by the Checkbox class in the java.awt package.
Key Features:
1. Constructor:
o Checkbox(String label): Creates a checkbox with the given label.
o Checkbox(String label, boolean state): Creates a checkbox with a predefined
state (checked or unchecked).
o Checkbox(String label, CheckboxGroup group, boolean state): Used for radio
button behavior.
2. Event Handling:
o ItemListener is used to detect checkbox state changes.
o The method itemStateChanged(ItemEvent e) is implemented to define the
action.
Example Program: AWT Checkbox
Below is a simple program that creates checkboxes and displays their selected states.
java
CopyEdit
import java.awt.*;
import java.awt.event.*;
public class SimpleCheckBoxAWT {
public static void main(String[] args) {
// Create frame
Frame frame = new Frame("AWT CheckBox Example");
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
// Create checkboxes
Checkbox cb1 = new Checkbox("Option 1");
Checkbox cb2 = new Checkbox("Option 2");
// Create label to display checkbox states
Label label = new Label("Select an option");
// Add item listener to checkboxes
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent e) {
String selected = "Selected: ";
if (cb1.getState()) selected += "Option 1 ";
if (cb2.getState()) selected += "Option 2 ";
label.setText(selected);
}
};
cb1.addItemListener(itemListener);
cb2.addItemListener(itemListener);
// Add components to frame
frame.add(cb1);
frame.add(cb2);
frame.add(label);
// Add window closing event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dispose();
}
});
// Set frame visibility
frame.setVisible(true);
}
}