0% found this document useful (0 votes)
5 views4 pages

Listener Programs

The document contains three Java Swing applications demonstrating event handling: ListenerDemo for key and mouse events, ActionEventDemo for button click events, and TextDemo for text input events. Each application sets up a JFrame, adds components, and implements relevant listeners to handle user interactions. The applications showcase how to update the GUI based on user actions such as key presses, mouse movements, and text input.

Uploaded by

vranjange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Listener Programs

The document contains three Java Swing applications demonstrating event handling: ListenerDemo for key and mouse events, ActionEventDemo for button click events, and TextDemo for text input events. Each application sets up a JFrame, adds components, and implements relevant listeners to handle user interactions. The applications showcase how to update the GUI based on user actions such as key presses, mouse movements, and text input.

Uploaded by

vranjange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
import [Link].*;
import [Link].*;

public class ListenerDemo extends JFrame implements KeyListener, MouseListener,


MouseMotionListener
{
private String message = "Press any key or use the mouse";
public ListenerDemo()
{
setTitle("Key and Mouse Event Handling");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
setFocusable(true);
}

// Paint method to display messages


public void paint(Graphics g)
{
[Link](g);
[Link](message, 50, 100);
}

// KeyListener methods
public void keyPressed(KeyEvent e)
{
message = "Key Pressed: " + [Link]();
repaint();
}

public void keyReleased(KeyEvent e)


{
message = "Key Released: " + [Link]();
repaint();
}

public void keyTyped(KeyEvent e)


{
message = "Key Typed: " + [Link]();
repaint();
}

// MouseListener methods
public void mouseClicked(MouseEvent e)
{
message = "Mouse Clicked at (" + [Link]() + ", " + [Link]() + ")";
repaint();
}

public void mousePressed(MouseEvent e)


{
message = "Mouse Pressed";
repaint();
}

public void mouseReleased(MouseEvent e)


{
message = "Mouse Released";
repaint();
}

public void mouseEntered(MouseEvent e)


{
message = "Mouse Entered Window";
repaint();
}

public void mouseExited(MouseEvent e)


{
message = "Mouse Exited Window";
repaint();
}

// MouseMotionListener methods
public void mouseDragged(MouseEvent e)
{
message = "Mouse Dragged at (" + [Link]() + ", " + [Link]() + ")";
repaint();
}

public void mouseMoved(MouseEvent e)


{
message = "Mouse Moved at (" + [Link]() + ", " + [Link]() + ")";
repaint();
}

// Main method
public static void main(String[] args)
{
ListenerDemo l=new ListenerDemo();
}
}
import [Link].*;
import [Link].*;
import [Link].*;

public class ActionEventDemo extends JFrame implements ActionListener


{
private JTextField tf;
private JButton bt;

public ActionEventDemo()
{
// Frame settings
setTitle("Action Event Example");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());

// Create components
tf = new JTextField(15);
bt= new JButton("Click Me");

// Add ActionListener to button


[Link](this);

// Add components to frame


add(tf);
add(bt);

setVisible(true);
}

// Handle ActionEvent
public void actionPerformed(ActionEvent e)
{
String text = [Link]();
[Link](this, "You entered: " + text);
}

// Main method
public static void main(String[] args)
{
new ActionEventDemo();
}
}
import [Link].*;
import [Link].*;
import [Link].*;

public class TextDemo extends JFrame


{
private JTextField tf;
private JLabel l;

public TextDemo()
{
setTitle("Text Event Example");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
tf = new JTextField(20);
l = new JLabel("Type something...");
// Add DocumentListener to handle text changes
[Link]().addDocumentListener(new DocumentListener()
{
public void insertUpdate(DocumentEvent e)
{
updateLabel();
}
public void removeUpdate(DocumentEvent e)
{
updateLabel();
}
public void changedUpdate(DocumentEvent e)
{
updateLabel();
}
private void updateLabel()
{
[Link]("Current Text: " + [Link]());
}
});
add(tf);
add(l);
setVisible(true);
}

public static void main(String[] args)


{
new TextDemo();
}
}

You might also like