当前位置: 技术问答>java相关
一个简单的问题,请大家帮忙
来源: 互联网 发布时间:2014-12-26
本文导语: 下面的程序老是提示错误:MarathonApp should be declare 哪位能告诉什么原因 package MarathonApp; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MarathonApp implements WindowListener { JLabel label = new JLabel("It...
下面的程序老是提示错误:MarathonApp should be declare
哪位能告诉什么原因
package MarathonApp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MarathonApp implements WindowListener
{
JLabel label = new JLabel("It just keeps going,and going...");
JFrame frame = new JFrame("MarathonApp Example");
public MarathonApp()
{
frame.addWindowListener (this);
frame.getContentPane().add (label);
frame.setSize(300,100);
frame.setVisible (true);
}
public void windowClosing(WindowEvent e)
{
System.exit (0);
}
}
哪位能告诉什么原因
package MarathonApp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MarathonApp implements WindowListener
{
JLabel label = new JLabel("It just keeps going,and going...");
JFrame frame = new JFrame("MarathonApp Example");
public MarathonApp()
{
frame.addWindowListener (this);
frame.getContentPane().add (label);
frame.setSize(300,100);
frame.setVisible (true);
}
public void windowClosing(WindowEvent e)
{
System.exit (0);
}
}
|
基本概念:你的MarathonApp类是从接口WindowListener来的,而接口WindowListener中有多个抽象方法,WindowListener想变成类则每个方法都必须override一下,查JDK1.3 document得到
interface WindowListener共有7个方法,所以在你的程序中再加入以下6个方法就可以了。
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){
}
可以查看Thinking in Java(version 1) chapter13的 Using listener adapters for simplicity小节
... But since WindowListener is an interface, you must implement all of the other methods even if they don’t do anything. This can be annoying.
To solve the problem, each of the listener interfaces that have more than one method are provided with adapters,...
interface WindowListener共有7个方法,所以在你的程序中再加入以下6个方法就可以了。
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){
}
可以查看Thinking in Java(version 1) chapter13的 Using listener adapters for simplicity小节
... But since WindowListener is an interface, you must implement all of the other methods even if they don’t do anything. This can be annoying.
To solve the problem, each of the listener interfaces that have more than one method are provided with adapters,...