Interface: MouseListener可以實作多種滑鼠事件、WindowListener可以得到關閉視窗的功能
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
public class mm extends Frame implements ActionListener, MouseListener, WindowListener{
ArrayList<Integer> buf = new ArrayList<Integer>();
Button[] bt = new Button[10];
mm(){
this.setSize(600,500);
this.setLocation(800,600);
this.setLayout(null);
/*--Button--*/
for(int i=0;i<10;i++){
bt[i] = new Button("O");
bt[i].setSize(80,40);
bt[i].setLocation(fun.Rand(0,599),fun.Rand(0,499));
bt[i].addActionListener(this);/*interface*/
bt[i].addMouseListener(this);/*interface*/
this.add(bt[i]);
bt[i].setVisible(true);
}
this.addWindowListener(this);/*interface*/
this.setVisible(true);
}
/*--main--*/
public static void main(String[] arg)
{
mm p = new mm();
}
/*--Action Event--*/
@Override
public void actionPerformed(ActionEvent e) {
/*if(e.getSource()==bt01){
bt01.setLabel("Aoch");
} */
}
/*--Mouse Event--*/
@Override
public void mouseClicked(MouseEvent e) {
((Button)(e.getSource())).setLabel("X");
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
if(e.getSource()==bt[0]){bt[0].setLocation(50,50);}
else if(e.getSource()==bt[1]){bt[1].setLocation(50,50);}
else if(e.getSource()==bt[2]){bt[2].setLocation(50,50);}
else if(e.getSource()==bt[3]){bt[3].setLocation(50,50);}
else if(e.getSource()==bt[4]){bt[4].setLocation(50,50);}
else{
((Button)(e.getSource())).setLocation(fun.Rand(0,599),fun.Rand(0,499));
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/*--Window Event--*/
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
dispose(); /*dispose即銷毀的意思,將視窗物件關閉*/
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}