Java by API/java.awt.event/MouseAdapter

Материал из Java эксперт
Версия от 14:18, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Anonymous MouseAdapter

import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = aWindow.getContentPane();
    content.add(new MouseMotionAnonymous());
    aWindow.setVisible(true);
  }
}
class MouseMotionAnonymous extends JPanel {
  public MouseMotionAnonymous() {
    setBackground(Color.white);
    addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent me) {
        setBackground(Color.white);
        repaint();
      }
    });
    addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseDragged(MouseEvent me) {
        setBackground(Color.cyan);
        repaint();
      }
    });
  }
}





extends MouseAdapter

import java.awt.Color;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame();
    aWindow.setBounds(200, 200, 200, 200);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = aWindow.getContentPane();
    content.add(new MouseMotionAdapterDemo());
    aWindow.setVisible(true);
  }
}
class MouseMotionAdapterDemo extends JPanel {
  public MouseMotionAdapterDemo() {
    setBackground(Color.white);
    addMouseListener(new Adapter1(this));
    addMouseMotionListener(new Adapter2(this));
  }
}
class Adapter1 extends MouseAdapter {
  MouseMotionAdapterDemo mmad;
  public Adapter1(MouseMotionAdapterDemo mmad) {
    this.mmad = mmad;
  }
  public void mouseReleased(MouseEvent me) {
    mmad.setBackground(Color.white);
    mmad.repaint();
  }
}
class Adapter2 extends MouseMotionAdapter {
  MouseMotionAdapterDemo mmad;
  public Adapter2(MouseMotionAdapterDemo mmad) {
    this.mmad = mmad;
  }
  public void mouseDragged(MouseEvent me) {
    mmad.setBackground(Color.cyan);
    mmad.repaint();
  }
}





new MouseAdapter()

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
  public MainClass() {
      addMouseListener(new MouseAdapter() { 
          public void mousePressed(MouseEvent me) { 
            System.out.println(me); 
          } 
        }); 
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}