2013年9月10日星期二

java monitor mouse events

I wrote a small program that monitors the mouse click event , it has not been a problem, unravel , however,
Please help me see, I am a novice,
Thank you !

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.awt.event.MouseListener;
import java.awt.Toolkit;
public class Wmain{
public static void main(String[] args){
JFrame myjf=new testchress();
}
}

public class testchress extends JFrame implements MouseListener {
public testchress(){
this.setVisible(true);
this.setTitle("我是标题");
this.setSize(300,200);//窗口宽300,高200
this.setLocation(400,400);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度
int height=Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度
this.setLocation((width-200)/2,(height-200)/2);
this.addMouseListener(this);
}
public void mouseClicked(MouseEvent e){
System.out.println("你单击了");
JOptionPane.showMessageDialog(this, "你单击了内容");
}

public void mousePressed(MouseEvent e){
System.out.println("你按下了键");
}

public void mouseReleased(MouseEvent e){
System.out.println("抬起了");
}

/**
* 进入事件
*/
public void mouseEntered(MouseEvent e){

}

//鼠标移出事件
public void mouseExited(MouseEvent e){

}
}

------ Solution ------------------------------------- -------
visual question should be: in the same file , the class can not have two public

recommend testchress not declared as public, delete the keyword.
------ Solution ---------------------------------------- ----
If two classes wrote a document compiled java file with the same name as the class is public , the other to remove the public; addition, less an implementation class method :

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Toolkit;
public class Wmain{
    public static void main(String[] args){
        JFrame myjf=new testchress();
    }
}

class testchress extends JFrame implements MouseListener {
    public testchress(){
        this.setVisible(true);
        this.setTitle("我是标题");
        this.setSize(300,200);//窗口宽300,高200
        this.setLocation(400,400);
        int width=Toolkit.getDefaultToolkit().getScreenSize().width;//获取屏幕宽度
        int height=Toolkit.getDefaultToolkit().getScreenSize().height;//获取屏幕高度
        this.setLocation((width-200)/2,(height-200)/2);
        this.addMouseListener(this);
    }
    public void mouseClicked(MouseEvent e){
        System.out.println("你单击了");
        JOptionPane.showMessageDialog(this, "你单击了内容");
    }
    
    public void mousePressed(MouseEvent e){
        System.out.println("你按下了键");
    }
    
    @Override
    public void mouseReleased(MouseEvent e){
        System.out.println("抬起了");
    }
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}
    
}

------ Solution ------------------------------------- -------
just beginning to learn to monitor when the class is very easy to confuse MouseListener, mouseClicked and MouseAdapter difference
mouse listener class classification :
MouseListener, MouseMotionListener,
MouseWheelListener, EventListener

MouseAdapter ( adapter ) object implements MouseListener Interface
mouseClicked method is an implementation of MouseListener
------ For reference only ----------------- ----------------------
less introduced a package:
import java.awt.event.MouseEvent;

I think MouseEvent class in the listener , alas . .

and multiple classes in a file one inside , the other canceled public

Then you can , thank you

没有评论:

发表评论