- Event Object merupakan object yang mendiskripsikan sebuah event yang di-trigger oleh event source.
- Event Handler merupakan method yang menerima event object dan melakukan respond yang sesuai dengan event object tersebut.
- Event Listener merupakan interface yang akan meng-handle event yang terjadi. setiap type event mempunyai interface yang bersesuaian. Listener tersebut harus diimplementasikan oleh class yang akan meng-handle event
- Event Source merupakan pembangkit / trigger sebuah event objects
berikut ini type type event yang ada di Java :
- ActionEvent
- ItemEvent
- WindowEvent
- ContainerEvent
- ComponentEvent
- FocusEvent
- TextEvent
- KeyEvent
- MouseEvent
- AdjustmentEvent
- ActionListener
- ItemListener
- WindowListener
- ContainerListener
- ComponentListener
- FocusListener
- TextListener
- KeyListener
- MouseListener
- MouseMotionListener
- AdjustmentListenerTulisan saya kali ini akan mencontohkan tentang Event Handler
ini source code nya :Dimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MessageDialog extends JFrame {
private JButton tombol, btn2, btn3, btn4, btn5;
public MessageDialog() {
super ("Event Handling");
Container container = getContentPane();
container.setLayout(new FlowLayout());
tombol = new JButton ("Message Dialog");
tombol.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showMessageDialog (null,"Contoh Message Dialog");
}
}
);
container.add(tombol);
btn2 = new JButton ("Warning Message");
btn2.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showConfirmDialog(null, "Contoh Warning Message","Peringatan",
JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
}
}
);
container.add(btn2);
btn3 = new JButton ("Question Message");
btn3.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showConfirmDialog(null, "Contoh Question Message","Pertanyaan",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
}
);
container.add(btn3);
btn4 = new JButton ("Information Message");
btn4.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent e) {
JOptionPane.showConfirmDialog(null, "Contoh Information Message","Informasi",
JOptionPane.NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
}
);
container.add(btn4);
btn5 = new JButton ("Input Dialog");
btn5.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent e) {
String a = JOptionPane.showInputDialog("Input Nama : ");
JOptionPane.showMessageDialog(null, a);
}
}
);
container.add(btn5);
setSize (200,300);
setLocationRelativeTo(null);
setVisible (true);
}
public static void main (String arg[]) {
MessageDialog test = new MessageDialog();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
0 komentar:
Posting Komentar