Minggu, 04 Desember 2011

Kajian 4: avent listener


  • ActionListener
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;


  public class ThreadPertama {
  public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300, 200);
  frame.setLayout(new FlowLayout());
  final JButton button = new JButton();
  button.setText("Start!");
  button.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    new Thread() {
     public void run() {
      for (int i = 0; i < 50; i++) {
       // system.out.println(i+"A");
       button.setText(i + "A");
       try {
        Thread.sleep(300);
       } catch (InterruptedException e) {
        e.printStackTrace();
        // TODO: handle exception
       }
      }
     };
    }.start();
   }
  });
  frame.add(button);
  final JButton button2 = new JButton();
  button2.setText("Click!");
  button2.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    new Thread() {
     public void run() {
      for (int j = 0; j < 50; j++) {
       // System.out.println(i + "A");
       button2.setText(j + "B");
       try {
        Thread.sleep(300);
       } catch (InterruptedException e) {
        // TODO: handle exception
        e.printStackTrace();
       }
      }
     };
    }.start();
   }
  });
  frame.add(button2);
  frame.setVisible(true);
 }
}



  • MouseListener
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;

  public class MyFrame {
  public static void main(String[] args) {
  final JFrame frame = new JFrame("Mouse Listener");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(300, 300);
  frame.setLocationRelativeTo(null);
  frame.setLayout(new FlowLayout());
  final JButton button = new JButton("click");
  button.addMouseListener(new MouseListener() {
   @Override
   public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub
   }
   @Override
   public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
   }
   @Override
   public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub
   }
   @Override
   public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub
    Random random = new Random();
    int x = Math.abs(random.nextInt(frame.getWidth()) - button.getWidth());
    int y = Math.abs(random.nextInt(frame.getHeight()) - button.getHeight());
    button.setLocation(x, y);
   }
   @Override
   public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
   }
  });
  frame.add(button);
  frame.setVisible(true);
 }
}
  • Mouse Motion Listener
Interface MouseMotionListener mendukung MouseListener. Menyediakan method-method yang akan memantau pergerakan mouse,seperti drag dan pemindahan mouse. MouseMotionListener mempunyai dua method untuk diimplementasikan.


import java.awt.*;
import java.awt.event.*;

public class MouseClick {
  Label lbl;
  public static void main(String[] args) {
  MouseClick MC = new MouseClick();
  }

  public MouseClick(){
  Frame f = new Frame("Checking the mouse click");
  Panel p = new Panel();
  Button button = new Button("Click Me");
  button.addMouseListener(new MyMouseListener());
  p.add(button, BorderLayout.NORTH);
  f.add(p,BorderLayout.NORTH);
  lbl = new Label("Roseindia.net");
  f.add(lbl, BorderLayout.CENTER);
  f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });
  f.setSize(400,400);
  f.setVisible(true);
  }
 
  public class MyMouseListener extends MouseAdapter{
  public void mouseClicked(MouseEvent me){
  String str = lbl.getText();
  if (str.equals("Roseindia.net")){
  lbl.setText("You have clicke the button.");
  }
  else if (str.equals("You have clicke the button.")){
  lbl.setText("Roseindia.net");
  }
  }
  }

Tidak ada komentar:

Posting Komentar