TUGAS PRAKTIKUM
PEMROGRAMAN JAVA II
EVENT HANDLING
Oleh :
Eva Yulian R.
123080254
Asisten / Coas :
Shiddieqy Y.Katili / Dimas A.
JURUSAN TEKNIK INFORMATIKA
FAKULTAS TEKNOLOGI INDUSTRI
UNIVERSITAS PEMBANGUNAN NASIONAL “VETERAN” YOGYAKARTA
2010
TUGAS
1. Login
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class login extends JFrame{
public JLabel luser,lpass;
public JTextField tuser;
public JPasswordField tpass;
public JButton bok,bcancel;
public login(){ // construktor
luser = new JLabel("user") ;
lpass = new JLabel("Password");
tuser = new JTextField(10);
tpass = new JPasswordField(10);
bok = new JButton("OK");
bcancel = new JButton ("CANCEL");
setLayout(new GridLayout(3,2));
add(luser);
add(tuser);
add(lpass);
add(tpass);
add(bok);
add(bcancel);
tuser.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent ke) {
}
public void keyPressed(KeyEvent ke) {
if(ke.getKeyChar()==KeyEvent.VK_ENTER){
tpass.requestFocus();
}
}
public void keyReleased(KeyEvent ke) {
}
});
tuser.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent fe) {
//mengaktifkan/noaktifkan objek
}
public void focusLost(FocusEvent fe) {
tuser.setEnabled(false);
}
});
bok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String user =" ";
String pass ="";
user = tuser.getText();
pass = tpass.getText();
if(user.equalsIgnoreCase("123")&&pass.equalsIgnoreCase("java2")){
JOptionPane.showMessageDialog(null,"Login Sukses","Pesan",JOptionPane.OK_OPTION);
new MenuUtama();
dispose();
}else{
JOptionPane.showMessageDialog(null,"Login Gagal,user" +tuser.getText()+"Tidak terdaftar", "Pesan",JOptionPane.ERROR_MESSAGE);
tuser.setEnabled(true);
tuser.requestFocus();
tpass.setEnabled(true);
tuser.setText(" ");
tpass.setText("");
}
}
});
setDefaultCloseOperation(3);
setSize(300,150);
setVisible(true);
setTitle("Login");
}
}
public class plogin{
public static void main(String[] args) {
new login();
}
}
Output :
Penjelasan :
import java.awt.GridLayout; // untuk fungsi layout
import java.awt.event.ActionEvent;//untuk fungsi event
import java.awt.event.ActionListener;//fungsi event
import java.awt.event.FocusEvent;// untuk fungsi event
import java.awt.event.FocusListener; // untuk fungsi event
import java.awt.event.KeyEvent; // untuk fungsi event
import java.awt.event.KeyListener; // untuk fungsi event
import javax.swing.JButton; //untuk fungsi Button
import javax.swing.JFrame; //untuk fungsi frame
import javax.swing.JLabel;//untuk fungsi label
import javax.swing.JOptionPane; // untuk fungsi option
import javax.swing.JPasswordField; // untuk fungsi password
import javax.swing.JTextField; // untuk fungsi text
class login extends JFrame{ // kelas
public JLabel luser,lpass;//deklarasi JLabel
public JTextField tuser;//deklarasi JTextField
public JPasswordField tpass;//deklarasi JPassword
public JButton bok,bcancel;//deklarasi JButton
public login(){ // construktor
luser = new JLabel("user"); // instansiasi label user
lpass = new JLabel("Password");// instansiasi label password
tuser = new JTextField(10); // instansiasi textfield user
tpass = new JPasswordField(10); // instansiasi textfield password
bok = new JButton("OK");// instansiasi button ok
bcancel = new JButton ("CANCEL");// instansiasi button cancel
setLayout(new GridLayout(3,2));// mengatur gridlayout
add(luser);//menambahkan label user pada layout
add(tuser);//menambahkan textfield user
add(lpass);//menambahkan label pass
add(tpass);// menambahkan textfield pass
add(bok); // menambahkan button ok
add(bcancel); // menambahkan button cancel
tuser.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent ke) { //method
}
public void keyPressed(KeyEvent ke) {//method
if(ke.getKeyChar()==KeyEvent.VK_ENTER)
tpass.requestFocus();
}
}
public void keyReleased(KeyEvent ke) {
}
});
tuser.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent fe) {
//mengaktifkan/noaktifkan objek
}
public void focusLost(FocusEvent fe) {
tuser.setEnabled(false);
}
});
bok.addActionListener(new ActionListener() {//menambahkb actionlistener pada button ok
public void actionPerformed(ActionEvent ae) {
String user =" ";
String pass ="";
user = tuser.getText();//mengambil nilai char pada user
pass = tpass.getText();//mengambil nilai char pada password
if(user.equalsIgnoreCase("123")&&pass.equalsIgnoreCase("java2")){ //username berisi 123 dan paswaord berisi java2
JOptionPane.showMessageDialog(null,"Login Sukses","Pesan",JOptionPane.OK_OPTION);//
new MenuUtama();//membuka menu utama
dispose();//befungsi untuk balik ke semula
}else{
JOptionPane.showMessageDialog(null,"Login Gagal,user" +tuser.getText()+"Tidak terdaftar", "Pesan",JOptionPane.ERROR_MESSAGE);//login gagal maka keluar pesan login gagal (user yang dimasukkan ) tidak terdaftar
tuser.setEnabled(true);
tuser.requestFocus();
tpass.setEnabled(true);
tuser.setText(" ");
tpass.setText("");
}
}
});
setDefaultCloseOperation(3);
setSize(300,150);//mengatur ukuran output
setVisible(true);//
setTitle("Login");
}
}
public class plogin{//kelas
public static void main(String[] args) {//method main
new login();//objek yg memanggil kelas
}
}
2. Mouse
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class Main extends JFrame implements MouseMotionListener{
JScrollPane sb;
JTextArea jt;
String text="";
public Main() {
setTitle("Mouse Motion Listener");
jt=new JTextArea();
sb=new JScrollPane();
sb.setViewportView(jt);
jt.setFont(new java.awt.Font("DejaVu Sans", 3, 16));
setLayout(null);
//sb.add(jt);
add(sb);
sb.setBounds(300,75, 260, 270);
setSize(600, 400);
setVisible(true);
setDefaultCloseOperation(3);
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseMoved(MouseEvent me) {
try{
text=text+ "\nPosisi Mouse x: "+getMousePosition().x+", y: "+getMousePosition().y;
jt.setText(text);}catch(Exception e){
}
}
public static void main(String[] args) {
new Main();
}
}
output -->
2. Mouse
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class Main extends JFrame implements MouseMotionListener{
JScrollPane sb;
JTextArea jt;
String text="";
public Main() {
setTitle("Mouse Motion Listener");
jt=new JTextArea();
sb=new JScrollPane();
sb.setViewportView(jt);
jt.setFont(new java.awt.Font("DejaVu Sans", 3, 16));
setLayout(null);
//sb.add(jt);
add(sb);
sb.setBounds(300,75, 260, 270);
setSize(600, 400);
setVisible(true);
setDefaultCloseOperation(3);
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseMoved(MouseEvent me) {
try{
text=text+ "\nPosisi Mouse x: "+getMousePosition().x+", y: "+getMousePosition().y;
jt.setText(text);}catch(Exception e){
}
}
public static void main(String[] args) {
new Main();
}
}
output -->
Tidak ada komentar:
Posting Komentar