Related Topics

JAVA Programing
import java.awt.*;
import java.awt.event.*;
public class MyTextField extends Frame implements FocusListener {
private TextField myTextField;
public MyTextField() {
myTextField = new TextField(20);
myTextField.addFocusListener(this);
add(myTextField);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new MyTextField();
}
// FocusListener methods
public void focusGained(FocusEvent e) {
System.out.println("Text field gained focus");
}
public void focusLost(FocusEvent e) {
System.out.println("Text field lost focus");
}
}
In this example, the MyTextField
class extends the Frame
class and implements the FocusListener
interface. The myTextField
variable is an instance of the TextField
class, and its addFocusListener
method registers MyTextField
object as a listener for focus events.
The focusGained
method is called when the myTextField
component gains focus, and it prints out a message to the console. The focusLost
method is called when the myTextField
component loses focus, and it also prints out a message to the console.
Overall, the FocusListener
interface is useful for creating interactive Java GUIs that respond to user input in a flexible and dynamic way.
import javax.swing.*;
import java.awt.event.*;
public class KeyEventsDemo extends JFrame implements KeyListener {
public KeyEventsDemo() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setFocusable(true);
addKeyListener(this);
}
public static void main(String[] args) {
KeyEventsDemo demo = new KeyEventsDemo();
demo.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println("Key Pressed: " + KeyEvent.getKeyText(keyCode));
}
@Override
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
System.out.println("Key Released: " + KeyEvent.getKeyText(keyCode));
}
@Override
public void keyTyped(KeyEvent e) {
char c = e.getKeyChar();
System.out.println("Key Typed: " + c);
}
}
In this example, the KeyEventsDemo
class extends the JFrame
class and implements the KeyListener
interface. The setSize()
, setDefaultCloseOperation()
, and setLocationRelativeTo()
methods set the size, close operation, and location of the frame. The setFocusable()
method is used to enable the frame to receive keyboard events, and the addKeyListener()
method is used to add the KeyListener
interface to the frame.
The keyPressed()
, keyReleased()
, and keyTyped()
methods are the three methods that implement the KeyListener
interface. These methods print out messages to the console when a key is pressed, released, or typed, respectively.
Overall, the KeyListener
interface provides a way to handle keyboard events in Java and create interactive GUIs that respond to user input in a flexible and dynamic way.
void mouseWheelMoved(MouseWheelEvent e)
This method is called when the mouse wheel is rotated. The MouseWheelEvent
object that is passed to the method contains information about the event, such as the amount that the wheel was rotated and the location of the cursor.
To use the MouseWheelListener
interface to handle mouse wheel events in Java, you need to create a class that implements the interface and override the mouseWheelMoved()
method. Here is an example:
import java.awt.*;
import java.awt.event.*;
public class MouseWheelDemo extends Frame implements MouseWheelListener {
public MouseWheelDemo() {
addMouseWheelListener(this);
setSize(300, 200);
setVisible(true);
}
public void mouseWheelMoved(MouseWheelEvent e) {
int rotation = e.getWheelRotation();
System.out.println("Mouse wheel rotated: " + rotation);
}
public static void main(String[] args) {
new MouseWheelDemo();
}
}
In this example, the MouseWheelDemo
class extends the Frame
class and implements the MouseWheelListener
interface. The addMouseWheelListener()
method is used to add the MouseWheelListener
interface to the frame. The setSize()
and setVisible()
methods are used to set the size and visibility of the frame.
The mouseWheelMoved()
method is called when the mouse wheel is rotated. In this example, the method prints out a message to the console that indicates the direction and amount of the rotation.
Overall, the MouseWheelListener
interface provides a way to handle mouse wheel events in Java and create GUIs that allow the user to scroll through content in a window or other container.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.