-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActionListenerExample.java
47 lines (37 loc) · 1.42 KB
/
ActionListenerExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*Como escrever o ActionListener
A abordagem comum é implementar o ActionListener.
Se você implementar a classe ActionListener, precisará seguir 3 etapas:
1) Implemente a interface do ActionListener na classe:
2) Registre o componente com o ouvinte:
3) Substitua o método actionPerformed():
*/
import javax.swing.*;
import java.awt.event.*;
public class ActionListenerExample implements ActionListener{
JTextField tf; // Declare tf here so it can be accessed in actionPerformed method
public static void main(String[] args) {
ActionListenerExample example = new ActionListenerExample(); // Create an instance of the class
example.createUI(); // Call the method to create the UI
}
public void createUI() {
JFrame f = new JFrame("Exemplo do ActionListener");
tf = new JTextField();
tf.setBounds(50, 50, 150, 20);
JButton b = new JButton("Clique Aqui");
b.setBounds(50, 100, 80, 30);
b.addActionListener(this); // Add the action listener to the button
f.add(b);
f.add(tf);
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
tf.setText("Bem-vindo ao Javatpoint.");
}
}