当前位置: 技术问答>java相关
我来问一个简单的问题,可是我就是不会:)
来源: 互联网 发布时间:2015-03-16
本文导语: 下面这一段程序编译不能通过,想不通:) import java.awt.*; import java.applet.*; import java.awt.event.*; import java.net.*; public class AppletTest1 extends Applet { private AppletContext context; private AudioClip audio; private TextField tf; priva...
下面这一段程序编译不能通过,想不通:)
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
public class AppletTest1 extends Applet
{
private AppletContext context;
private AudioClip audio;
private TextField tf;
private Button btPlay,btBrowse;
private TextArea ta;
private boolean pause;
public void init()
{
setLayout(new GridLayout(3,1));
context=getAppletContext();
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p3.setLayout(new BorderLayout());
p1.add(new Label("URL: "));
p1.add(tf=new TextField(getParameter("location")));
p2.add(btPlay=new Button("Play"));
p2.add(btBrowse=new Button("Browse"));
ActionListen act=new ActionListen(this);
btPlay.addActionListener(act);
btBrowse.addActionListener(act);
ta=new TextArea(null,10,20,TextArea.SCROLLBARS_VERTICAL_ONLY);
p3.add(ta,BorderLayout.CENTER);
add(p1);
add(p2);
add(p3);
ta.append("Applet initializedn");
pause=false;
}
public void start()
{
ta.append("Applet startedn");
if (pause)
{
pause=false;
playAudio();
}
}
public void stop()
{
ta.append("Applet stoppedn");
if (btPlay.getLabel().compareTo("STOP")==0)
{
stopAudio();
pause=true;
}
}
public void playAudio()
{
try
{
audio=getAudioClip(new URL(tf.getText()));
audio.loop();
btPlay.setLabel("STOP");
}
catch (MalformedURLException e)
{
ta.append("Wrong URL......");
}
}
public void stopAudio()
{
btPlay.setLabel("PLAY");
audio.stop();
}
public void browse()
{
try
{
context.showDocument(new URL(tf.getText()),"_blank");
}
catch (MalformedURLException e)
{
ta.append("Wrong URL......");
}
}
class ActionListen implements ActionListener
{
private AppletTest1 applet; //建立与AppletTest1的沟通
public void ActionListen(AppletTest1 app)
{
applet=app;
}
public void actionPerformed(ActionEvent e)
{
String label=((Button)e.getSource()).getLabel();
if (label.compareTo("PLAY")==0)
applet.playAudio();
else if (label.compareTo("BROWSE")==0)
applet.browse();
else
applet.stopAudio();
}
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
public class AppletTest1 extends Applet
{
private AppletContext context;
private AudioClip audio;
private TextField tf;
private Button btPlay,btBrowse;
private TextArea ta;
private boolean pause;
public void init()
{
setLayout(new GridLayout(3,1));
context=getAppletContext();
Panel p1=new Panel();
Panel p2=new Panel();
Panel p3=new Panel();
p3.setLayout(new BorderLayout());
p1.add(new Label("URL: "));
p1.add(tf=new TextField(getParameter("location")));
p2.add(btPlay=new Button("Play"));
p2.add(btBrowse=new Button("Browse"));
ActionListen act=new ActionListen(this);
btPlay.addActionListener(act);
btBrowse.addActionListener(act);
ta=new TextArea(null,10,20,TextArea.SCROLLBARS_VERTICAL_ONLY);
p3.add(ta,BorderLayout.CENTER);
add(p1);
add(p2);
add(p3);
ta.append("Applet initializedn");
pause=false;
}
public void start()
{
ta.append("Applet startedn");
if (pause)
{
pause=false;
playAudio();
}
}
public void stop()
{
ta.append("Applet stoppedn");
if (btPlay.getLabel().compareTo("STOP")==0)
{
stopAudio();
pause=true;
}
}
public void playAudio()
{
try
{
audio=getAudioClip(new URL(tf.getText()));
audio.loop();
btPlay.setLabel("STOP");
}
catch (MalformedURLException e)
{
ta.append("Wrong URL......");
}
}
public void stopAudio()
{
btPlay.setLabel("PLAY");
audio.stop();
}
public void browse()
{
try
{
context.showDocument(new URL(tf.getText()),"_blank");
}
catch (MalformedURLException e)
{
ta.append("Wrong URL......");
}
}
class ActionListen implements ActionListener
{
private AppletTest1 applet; //建立与AppletTest1的沟通
public void ActionListen(AppletTest1 app)
{
applet=app;
}
public void actionPerformed(ActionEvent e)
{
String label=((Button)e.getSource()).getLabel();
if (label.compareTo("PLAY")==0)
applet.playAudio();
else if (label.compareTo("BROWSE")==0)
applet.browse();
else
applet.stopAudio();
}
}
}
|
class ActionListen implements ActionListener
{
private AppletTest1 applet; //建立与AppletTest1的沟通
//public void ActionListen(AppletTest1 app)
public ActionListen(AppletTest1 app)
{
applet=app;
}
.......................................
构造函数没有返回值
{
private AppletTest1 applet; //建立与AppletTest1的沟通
//public void ActionListen(AppletTest1 app)
public ActionListen(AppletTest1 app)
{
applet=app;
}
.......................................
构造函数没有返回值
|
Constructors are different from normal methods in some important ways:
1.The method name is the same as the class name.
2.There is no return type.
3.Constructors can't be inherited as other superclass methods are.
4.The constructor method can't be final,abstract,synchronized,native,or static
1.The method name is the same as the class name.
2.There is no return type.
3.Constructors can't be inherited as other superclass methods are.
4.The constructor method can't be final,abstract,synchronized,native,or static
|
ActionListen act=new ActionListen();
act.ActionListen(this);
act.ActionListen(this);
|
我弄不明白,你为什么不这样做:
public class AppletTest1 extends Applet implements ActionListener{
...
public class AppletTest1 extends Applet implements ActionListener{
...