0% found this document useful (0 votes)
39 views

Modul Ii: Penanganan Kejadian (Event)

The document discusses event handling in Java programs. It provides examples of common event listeners like ActionListener, MouseListener, and WindowListener that respond to user interactions. It then gives 4 examples of Java programs that demonstrate using events and event listeners to trigger actions when buttons are clicked or text is entered. The examples show how to add event listeners to buttons and respond to button clicks by displaying messages or changing label text.

Uploaded by

Pandy van Yusuf
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Modul Ii: Penanganan Kejadian (Event)

The document discusses event handling in Java programs. It provides examples of common event listeners like ActionListener, MouseListener, and WindowListener that respond to user interactions. It then gives 4 examples of Java programs that demonstrate using events and event listeners to trigger actions when buttons are clicked or text is entered. The examples show how to add event listeners to buttons and respond to button clicks by displaying messages or changing label text.

Uploaded by

Pandy van Yusuf
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

MODUL II

PENANGANAN KEJADIAN (EVENT)

Event merupakan suatu kejadian yang dilakukan oleh user terhadap user interface. Setiap
objek dapat dinotifikasi jika suatu event terjadi sehingga objek tersebut dapat
memutuskan apa yang harus dilakukan untuk menanggapi event yang bersangkutan.
Beberapa contoh event :
Event Listeners Deskripsi
ActionListener Bereaksi atas perubahan mouse atau keyboard
MouseListener Bereaksi atas pergerakan mouse
WindowListener Bereaksi atas perubahan window.

Contoh 1:
import java.awt.event.*;
import javax.swing.*;

public class klik extends JFrame implements ActionListener {


JButton tombol = new JButton ("Click Me!");

public klik() {
tombol.addActionListener(this);
add(tombol);
setSize (200,100);
setVisible (true);
}

public static void main (String args []) {


klik test = new klik();
}

public void actionPerformed (ActionEvent e) {


if (e.getSource() == tombol) {
JOptionPane.showMessageDialog(null, "Pemrograman Visual");
}
}
}

Output :

Contoh 2 :
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

class action1 extends JFrame implements ActionListener {


JButton bttn = new JButton("Click me");
JLabel lbl = new JLabel();
public action1() {
super("Button");
setSize(500, 400);
setVisible(true);
setLayout(null);

add(bttn);
bttn.setBounds(180, 100, 150, 25);
add(lbl);
lbl.setBounds(10,150,475,55);

lbl.setFont(new Font("Tahoma", Font.BOLD, 22));


bttn.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


{ if(e.getSource() == bttn)
{ lbl.setText("Anda telah menekan sebuah tombol");
}
}
public static void main(String[] args)
{ new action1();
}
}
Output :

Contoh 3 :
import javax.swing.*;
import java.awt.event.*;

class fun3 extends JFrame implements ActionListener{


JLabel l1=new JLabel("Tuliskan Teks :");
JLabel l2=new JLabel();
JTextField t1=new JTextField();
JButton b1=new JButton("Tampil");

fun3(){
l1.setBounds(10,10,100,30);
t1.setBounds(10,50,250,30);
b1.setBounds(50,90,80,30);
l2.setBounds(10,130,250,30);

add(l1);
add(l2);
add(t1);
add(b1);
b1.addActionListener(this);

setSize(280,250);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e){


if (e.getSource()==b1){
String isi=t1.getText();
l2.setText(isi);
}
}

public static void main (String [] args){


fun3 f3=new fun3();
}
}

Output :

Contoh 4 :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class loginNonDB extends JFrame implements ActionListener {


JLabel lblUser=new JLabel("User"), lblPass=new JLabel("Password");
JTextField txtUser=new JTextField();
JTextField txtPass =new JTextField();
JButton btnLogin=new JButton("log in");

public static void main(String [] args) {


loginNonDB log = new loginNonDB();
log.setVisible(true);
}

loginNonDB() {
super("Login");
setSize(250,200);
setLayout(null);
lblUser.setBounds(15,20,100,25);
lblPass.setBounds(15,55,100,25);
txtUser.setBounds(90,20,120,25);
txtPass.setBounds(90,55,120,25);
btnLogin.setBounds(80,110,85,25);

btnLogin.addActionListener(this);
add(lblUser);
add(txtUser);
add(lblPass);
add(txtPass);
add(btnLogin);
setLocationRelativeTo(null);
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {


if(ae.getSource()==btnLogin) {
if (txtUser.getText().equals("admin") &&
txtPass.getText().equals("root")) {
javax.swing.JOptionPane.showMessageDialog(null, "Login Berhasil
\nAnda berhasil masuk", "Konfirmasi",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}

else {
javax.swing.JOptionPane.showMessageDialog(null, "User anda tidak
dikenal","Kesalahan",javax.swing.JOptionPane.ERROR_MESSAGE);
txtUser.setText("");
txtUser.requestFocus();
System.exit(0);
}
}
}
}
Output :

You might also like