如何将程序写入java applet?
publicclassClientextendsFrame{TextFieldtf=newTextField(20);TextAreata=newTextArea();B...
public class Client extends Frame
{
TextField tf=new TextField(20);
TextArea ta=new TextArea();
Button send=new Button("send");
Button voiceChat=new Button("接受语音");
Socket client;
InputStream in;
OutputStream out;
BufferedReader br;
BufferedWriter bw;
public Client()
{
super("Client");
add("North",tf);
add("Center",ta);
add("South",send);
add("East",voiceChat);
setSize(250,250);
show();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
try
{
Socket client=new Socket("127.0.0.1",5000);
ta.append("Connect to:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
out=client.getOutputStream();
bw=new BufferedWriter(new OutputStreamWriter(out));
}
catch(IOException ioe)
{}
while(true)
{
try
{
byte[] buf=new byte[200];
in.read(buf);
String str=new String(buf);
ta.append("Server say:"+str);
ta.append("\n");
}
catch(IOException e)
{
System.out.print(e.getMessage());
}
}
}
public boolean action(Event evt, Object arg)
{
if(evt.target.equals(send))
{
try
{
String str=tf.getText();
byte[] buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("I say:"+str);
ta.append("\n");
}
catch(IOException ioe)
{
System.out.print(ioe.getMessage());
}
}
else if(evt.target.equals(voiceChat))
{
try
{
Socket cli=new Socket("127.0.0.1",6000);
Capture cap=new Capture(cli);
cap.start();
}
catch(Exception e)
{}
}
return true;
}
public static void main(String[] args)
{
Client client=new Client();
}
}
这是源程序,该如何写入java applet?我给忘了,试过几个都出错了。。。 展开
{
TextField tf=new TextField(20);
TextArea ta=new TextArea();
Button send=new Button("send");
Button voiceChat=new Button("接受语音");
Socket client;
InputStream in;
OutputStream out;
BufferedReader br;
BufferedWriter bw;
public Client()
{
super("Client");
add("North",tf);
add("Center",ta);
add("South",send);
add("East",voiceChat);
setSize(250,250);
show();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
try
{
Socket client=new Socket("127.0.0.1",5000);
ta.append("Connect to:"+client.getInetAddress().getHostName()+"\n\n");
in=client.getInputStream();
br=new BufferedReader(new InputStreamReader(in));
out=client.getOutputStream();
bw=new BufferedWriter(new OutputStreamWriter(out));
}
catch(IOException ioe)
{}
while(true)
{
try
{
byte[] buf=new byte[200];
in.read(buf);
String str=new String(buf);
ta.append("Server say:"+str);
ta.append("\n");
}
catch(IOException e)
{
System.out.print(e.getMessage());
}
}
}
public boolean action(Event evt, Object arg)
{
if(evt.target.equals(send))
{
try
{
String str=tf.getText();
byte[] buf=str.getBytes();
tf.setText(null);
out.write(buf);
ta.append("I say:"+str);
ta.append("\n");
}
catch(IOException ioe)
{
System.out.print(ioe.getMessage());
}
}
else if(evt.target.equals(voiceChat))
{
try
{
Socket cli=new Socket("127.0.0.1",6000);
Capture cap=new Capture(cli);
cap.start();
}
catch(Exception e)
{}
}
return true;
}
public static void main(String[] args)
{
Client client=new Client();
}
}
这是源程序,该如何写入java applet?我给忘了,试过几个都出错了。。。 展开
1个回答
展开全部
把第一行代码
public class Client extends Frame
改成
public class Client extends JApplet
试试看
public class Client extends Frame
改成
public class Client extends JApplet
试试看
更多追问追答
追问
我改过,不行的,似乎还需要paint方法跟init?反正一直在报错啊。。。。
追答
确实有点问题。
1)applet 里不用main() , 真正在网页上加载的时候是init->start->run->stop->destory的生命周期吧(记不清了,你可以查下applet的生命周期)
2)init()里进行初始化操作。
3)applet 是一个嵌入网页的小程序,不算一个window,所以没有addWindowListener(),这部分已经注释掉,(注释中的dispose 换成了 destroy)
4)action() 没发现有调用的地方,没修改。
修改后代码,超过1000字,帖不出来, 站内信发给你
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询