Tratarea evenimentelor
Tratarea evenimentelor pentru o aplicatie cu interfata grafica presupune realizarea urmatorilor pasi:
• declararea unei clase (interceptor de evenimente) care implementeaza interfete de tip listener (interfetele de tip listener contin metode care trebuie implementate pentru tratarea evenimentelor);
• implementarea tuturor metodelor definite in interfetele de tip listener;
• inregistrarea de obiecte de tip interceptor de evenimente de catre fiecare dintre componentele grafice pentru care se doreste tratarea evenimentelor, prin metode de tip add<Type>Listener().
Intr-o aplicatie cu interfata grafica in procesul de tratare a evenimentelor intervin clase de tip Event, respectiv Listener.
package ro.virtualcampus.ev; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class Contor1 extends JFrame implements ActionListener { | |
private static final long serialVersionUID = 1L; | |
JButton buton; | |
JLabel eticheta; | |
static int contor = 0; | |
public Contor1() { | |
super("Contor"); | |
setLayout(new GridLayout(2, 1)); | |
setLocation(100, 100); | |
buton = new JButton("Apasa!"); | |
eticheta = new JLabel("", JLabel.CENTER); | |
getContentPane().add(buton); | |
getContentPane().add(eticheta); | |
buton.addActionListener(this); | |
} | |
public void actionPerformed(ActionEvent e) { | |
if (e.getActionCommand().equals("Apasa!")) { | |
contor++; | |
eticheta.setText(contor + ""); | |
} | |
} | |
public static void main(String args[]) { | |
Contor1 app = new Contor1(); | |
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
app.pack(); | |
app.setVisible(true); | |
} | |
} |
Utilizarea claselor anonime in tratarea evenimentelor
O clasa anonima este o clasa interna folosita doar pentru instantierea unui singur obiect. Exemplul Contor.java poate fi rescris cu ajutorul claselor interne anonime.
package ro.virtualcampus.ev; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class Contor2 extends JFrame { | |
private static final long serialVersionUID = 1L; | |
JButton buton; | |
JLabel eticheta; | |
static int contor = 0; | |
public Contor2() { | |
super("Contor"); | |
setLocation(100, 100); | |
setLayout(new GridLayout(2, 1)); | |
buton = new JButton("Apasa!"); | |
eticheta = new JLabel("", JLabel.CENTER); | |
getContentPane().add(buton); | |
getContentPane().add(eticheta); | |
buton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
if (e.getActionCommand().equals("Apasa!")) { | |
contor++; | |
eticheta.setText(contor + ""); | |
} | |
} | |
}); | |
} | |
public static void main(String args[]) { | |
Contor2 app = new Contor2(); | |
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
app.pack(); | |
app.setVisible(true); | |
} | |
} |
Utilizarea claselor interne in tratarea evenimentelor
O clasa interna este o clasa declarata in interiorul unei alte clase. Exemplul precedent (Contor2.java) poate fi rescris cu ajutorul unei clase interne.
package ro.virtualcampus.ev; | |
import java.awt.GridLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
public class Contor3 extends JFrame { | |
private static final long serialVersionUID = 1L; | |
JButton buton; | |
JLabel eticheta; | |
static int contor = 0; | |
public Contor3() { | |
super("Contor"); | |
setLocation(100, 100); | |
setLayout(new GridLayout(2, 1)); | |
buton = new JButton("Apasa!"); | |
eticheta = new JLabel("", JLabel.CENTER); | |
getContentPane().add(buton); | |
getContentPane().add(eticheta); | |
buton.addActionListener(new ButtonListener()); | |
} | |
class ButtonListener implements ActionListener { | |
public void actionPerformed(ActionEvent e) { | |
if (e.getActionCommand().equals("Apasa!")) { | |
contor++; | |
eticheta.setText(contor + ""); | |
} | |
} | |
} | |
public static void main(String args[]) { | |
Contor3 app = new Contor3(); | |
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
app.pack(); | |
app.setVisible(true); | |
} | |
} |