一个JAVA初学者的问题
我的代码如下:importjava.awt.*;importjava.awt.event.*;classmyframe{publicstaticvoidmain(Stri...
我的代码如下:
import java.awt.*;
import java.awt.event.*;
class myframe
{
public static void main(String[] args)
{
Frame f = new Frame("lxpsnow");
f.setSize(400,400);
f.setBackground(Color.red);
f.setLocation(100,200);
f.addWindowListener(new wind());
f.show();
}
}
class wind implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
为什么编译后提示:wind不是抽象的,并且未覆盖java.awt.WindowListener 中的抽象方法 展开
import java.awt.*;
import java.awt.event.*;
class myframe
{
public static void main(String[] args)
{
Frame f = new Frame("lxpsnow");
f.setSize(400,400);
f.setBackground(Color.red);
f.setLocation(100,200);
f.addWindowListener(new wind());
f.show();
}
}
class wind implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
为什么编译后提示:wind不是抽象的,并且未覆盖java.awt.WindowListener 中的抽象方法 展开
1个回答
展开全部
记住,
如果你的类"不是抽象类"继承一个抽象类或者实现了某些接口,你就必须实现这些抽象类和接口定义的"所有"的抽象方法.请注意这里的用词是"所有".
所以,你的类wind不是抽象类,就必须实现所有在WindowListener 接口定义的"所有"的抽象方法.而你并没有现实所有的抽象方法,所以就报错.
WindowListener 的定义如下:(你只实现了windowClosing方法!)
public interface WindowListener extends EventListener {
public void windowOpened(WindowEvent e);
public void windowClosing(WindowEvent e);
public void windowClosed(WindowEvent e);
public void windowIconified(WindowEvent e);
public void windowDeiconified(WindowEvent e);
public void windowActivated(WindowEvent e);
public void windowDeactivated(WindowEvent e);
}
解决办法:
1.你可以在你的wind类里面加入这些方法:
于是类wind变成这样:
class wind implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
2.我估计你也许不想实现所有的WindowListener的抽象方法,因为没有必要去实现,那么请你参考以下的代码:(使用调配器,实现相同的功能)
import java.awt.*;
import java.awt.event.*;
class myframe
{
public static void main(String[] args)
{
Frame f = new Frame("lxpsnow");
f.setSize(400,400);
f.setBackground(Color.red);
f.setLocation(100,200);
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
f.show();
}
private static void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
}
如果你的类"不是抽象类"继承一个抽象类或者实现了某些接口,你就必须实现这些抽象类和接口定义的"所有"的抽象方法.请注意这里的用词是"所有".
所以,你的类wind不是抽象类,就必须实现所有在WindowListener 接口定义的"所有"的抽象方法.而你并没有现实所有的抽象方法,所以就报错.
WindowListener 的定义如下:(你只实现了windowClosing方法!)
public interface WindowListener extends EventListener {
public void windowOpened(WindowEvent e);
public void windowClosing(WindowEvent e);
public void windowClosed(WindowEvent e);
public void windowIconified(WindowEvent e);
public void windowDeiconified(WindowEvent e);
public void windowActivated(WindowEvent e);
public void windowDeactivated(WindowEvent e);
}
解决办法:
1.你可以在你的wind类里面加入这些方法:
于是类wind变成这样:
class wind implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
2.我估计你也许不想实现所有的WindowListener的抽象方法,因为没有必要去实现,那么请你参考以下的代码:(使用调配器,实现相同的功能)
import java.awt.*;
import java.awt.event.*;
class myframe
{
public static void main(String[] args)
{
Frame f = new Frame("lxpsnow");
f.setSize(400,400);
f.setBackground(Color.red);
f.setLocation(100,200);
f.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
f.show();
}
private static void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询