为什么不执行processMouseEvent方法

时间:2017-10-10 14:34:25

标签: java swing

我创建了一个只有黄色矩形的MyComponent类。我还重写了processMouseEvent以为组件添加鼠标事件。我的代码是:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import static javax.swing.JFrame.*;

public class MyComponent extends JComponent{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            JFrame frame=new JFrame("Cua so");
        frame.setLayout(null);
        MyComponent myComponent;
        myComponent = new MyComponent();
        myComponent.setBounds(2, 2, 200, 40);
        frame.add(myComponent);
        frame.setSize(400,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation  (EXIT_ON_CLOSE); 
        }
    });
    System.out.println("<Main>");
}
    public MyComponent()
    {
        this.enableEvents(MouseEvent.MOUSE_EVENT_MASK);
    }
    protected void ProcessMouseEvent(MouseEvent e)
    {
        System.out.println("<Process mouse event>");
        switch(e.getID())
        {
            case MouseEvent.MOUSE_PRESSED:
                System.out.println("<Mouse pressed>");
                break;
            case MouseEvent.MOUSE_RELEASED:
                System.out.println("<Mouse released>");
                break;
            case MouseEvent.MOUSE_DRAGGED:
                System.out.println("<Mouse dragged>");
                break;
        }
    }
    protected void paintComponent(Graphics g)
    {
        g.setColor(Color.YELLOW);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}

并且我不知道为什么processMouseEvent中的代码没有被执行。有人可以解释一下,我不想使用addMouseListener 谢谢你的阅读!

0 个答案:

没有答案