import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JTextArea; public class MouseListenerExample extends JFrame { public MouseListenerExample() { JTextArea textArea = new JTextArea("Click me!"); textArea.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { System.out.println("Mouse clicked!"); } }); getContentPane().add(textArea); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MouseListenerExample(); } }
import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JTextArea; public class MouseListenerExample extends JFrame { public MouseListenerExample() { JTextArea textArea = new JTextArea("Click me!"); textArea.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) { textArea.setText("Mouse entered!"); } public void mouseExited(MouseEvent e) { textArea.setText("Mouse exited!"); } }); getContentPane().add(textArea); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MouseListenerExample(); } }This example creates a JFrame window that contains a JTextArea with the text "Click me!". It adds a mouse listener to the JTextArea that changes the text to "Mouse entered!" when the user hovers over the component, and changes it back to "Click me!" when the user stops hovering. The package library used in these examples is the javax.swing package, which is part of the Java standard library.