问题描述
packageorg.liky.game.test;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjava.awt.image.BufferedImage;importjavax.imageio.ImageIO;importjavax.swing.JFrame;publicclassMyChessFrameextendsJFrameimplementsMouseListener{//privatestaticfinallongserialVersionUID=1L;publicMyChessFrame(){this.setTitle("五子");this.setSize(200,100);this.setResizable(false);//设置窗体是否可以改变大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭方式,关闭窗体时同时结束程序intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;intheight=Toolkit.getDefaultToolkit().getScreenSize().height;this.setLocation((width-200)/2,(height-100)/2);//设置窗体初始显示位置this.addMouseListener(this);this.setVisible(true);//窗体是否显示}publicvoidpaint(Graphicsg){//g.drawString("五子棋游戏",20,40);//绘制字符串//g.drawOval(20,40,40,50);//g.fillOval(20,40,40,40);//g.drawLine(20,40,80,40);//g.drawRect(20,40,40,20);//g.fillRect(80,40,40,20);BufferedImageimage=null;try{image=ImageIO.read(newFile("E:/桌面/11200001.jpg"));}catch(IOExceptione){e.printStackTrace();}g.drawImage(image,0,0,this);//g.drawRect(20,40,40,20);g.setColor(Color.blue);g.setFont(newFont("宋体",40,40));g.fillRect(80,40,40,20);g.drawString("五子棋游戏",20,100);}/*publicvoidmouseClicked(MouseEvente){System.out.println("鼠标点击");JOptionPane.showMessageDialog(this,"鼠标点击");}*/}
packageorg.liky.game.test;importjavax.swing.*;importjava.awt.*;publicclassTest{publicstaticvoidmain(String[]args){MyChessFramemf=newMyChessFrame();}
运行后出现这样的问题Exceptioninthread"AWT-EventQueue-0"java.lang.Error:Unresolvedcompilationproblem:ThetypeMyChessFramemustimplementtheinheritedabstractmethodMouseListener.mouseEntered(MouseEvent)atorg.liky.game.test.MyChessFrame.mouseEntered(MyChessFrame.java:10)atjava.awt.Component.processMouseEvent(UnknownSource)atjava.awt.Component.processEvent(UnknownSource)atjava.awt.Container.processEvent(UnknownSource)atjava.awt.Window.processEvent(UnknownSource)atjava.awt.Component.dispatchEventImpl(UnknownSource)atjava.awt.Container.dispatchEventImpl(UnknownSource)atjava.awt.Window.dispatchEventImpl(UnknownSource)atjava.awt.Component.dispatchEvent(UnknownSource)atjava.awt.EventQueue.dispatchEvent(UnknownSource)atjava.awt.EventDispatchThread.pumpOneEventForFilters(UnknownSource)atjava.awt.EventDispatchThread.pumpEventsForFilter(UnknownSource)atjava.awt.EventDispatchThread.pumpEventsForHierarchy(UnknownSource)atjava.awt.EventDispatchThread.pumpEvents(UnknownSource)atjava.awt.EventDispatchThread.pumpEvents(UnknownSource)atjava.awt.EventDispatchThread.run(UnknownSource)
解决方案
解决方案二:
非抽象类实现接口时要实现接口的中的所有方法,MouseListener中有5个方法都要实现(当然没有用到的可以写出来,方法体为空)。当然这样写,有些麻烦,所以可以继承MouseAdapter类,然后选择自己要是实现的方法实现!
解决方案三:
第一种解决方法,实现所有的方法。//packageorg.liky.game.test;importjava.io.*;importjava.awt.*;importjava.awt.event.*;importjava.awt.image.BufferedImage;importjavax.imageio.ImageIO;importjavax.swing.JFrame;publicclassMyChessFrameextendsJFrameimplementsMouseListener{//privatestaticfinallongserialVersionUID=1L;publicMyChessFrame(){this.setTitle("五子");this.setSize(200,100);this.setResizable(false);//设置窗体是否可以改变大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭方式,关闭窗体时同时结束程序intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;intheight=Toolkit.getDefaultToolkit().getScreenSize().height;this.setLocation((width-200)/2,(height-100)/2);//设置窗体初始显示位置this.addMouseListener(this);this.setVisible(true);//窗体是否显示}publicvoidpaint(Graphicsg){//g.drawString("五子棋游戏",20,40);//绘制字符串//g.drawOval(20,40,40,50);//g.fillOval(20,40,40,40);//g.drawLine(20,40,80,40);//g.drawRect(20,40,40,20);//g.fillRect(80,40,40,20);BufferedImageimage=null;try{image=ImageIO.read(newFile("E:/桌面/11200001.jpg"));}catch(IOExceptione){e.printStackTrace();}g.drawImage(image,0,0,this);//g.drawRect(20,40,40,20);g.setColor(Color.blue);g.setFont(newFont("宋体",40,40));g.fillRect(80,40,40,20);g.drawString("五子棋游戏",20,100);}publicvoidmouseClicked(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmouseEntered(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmouseExited(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmousePressed(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmouseReleased(MouseEvente){//TODOAuto-generatedmethodstub}/**publicvoidmouseClicked(MouseEvente){System.out.println("鼠标点击");*JOptionPane.showMessageDialog(this,"鼠标点击");}*/}
解决方案四:
其实具体的这个程序上,只能有上面的一种方法,因为java中是单继承的!
解决方案五:
楼主实现MouseListener类没实现所有方法,编译器不提示错误嘛?难道楼主用记事本写的???
解决方案六:
楼上正解,如果一个类实现了一个接口,那它就要实现接口里面的所有方法。