使用MouseListeners导入JButton和JFrame时遇到问题

时间:2014-10-27 02:30:07

标签: java swing compiler-errors mouselistener mousemotionlistener

我正在制作一个绘图程序,需要同时实现MouseMotionListenerMouseListener。它还需要javax.swing.JFramejavax.swing.JButton

如果我自己导入JFrameJButton,则没有编译错误(除了不理解按钮/帧)。但是,如果我同时导入两者,则会在MouseListenerMouseMotionListener的双重实施中出错。

收到的错误是:

MyPaint is not abstract and does not override abstract method 
    mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener

导入javax.swing.*;无法解决问题,我感到很茫然。

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



public class MyPaint extends Canvas implements MouseListener, MouseMotionListener
{
    public boolean bg;

    public static JButton brushSize1 = new JButton("Size 1");
    public static JButton brushSize2 = new JButton("Size 2");
    public static JButton brushSize3 = new JButton("Size 3");

    //all the code necessary

2 个答案:

答案 0 :(得分:2)

MyPaint is not abstract and does not override abstract method 
    mouseExited(java.awt.event.MouseEvent) in java.awt.event.MouseListener

这意味着代码声明了接口的实现 - 但实际实现了接口中定义的所有方法。要摆脱该错误,请定义方法!

答案 1 :(得分:0)

MouseListener MouseMotionListener 都是接口。因此,如果要实现它们,则必须在接口中定义所有方法。像这样:

public void mouseExited(MouseEvent e){
    // Do what you want to do with this.
}

像这样,您必须在界面中定义 ALL 方法。