永发信息网

java聊天小工具出现问题,请帮忙参考下。。。

答案:1  悬赏:10  手机版
解决时间 2021-04-19 23:28
  • 提问者网友:你挡着我发光了
  • 2021-04-19 16:39

Server端:

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class TalkServer
{
public static void main(String[] args)
{
new MyFrame("Server Talking:");
}
}
class MyFrame extends Frame
{
TextField content,sendText;
Button send;
Panel p;
ServerSocket server=new ServerSocket(8887);
Socket s=server.accept();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
MyFrame(String title)
{
super(title);
content=new TextField(" ");
sendText=new TextField(" ");
send=new Button("发送");
send.addActionListenner(new ActionMonitor());
p=new Panel(new GridLayout(1,2));
add(content,"North");
p.add(sendText);
p.add(send);
add(p,"South");
pack();
addActionListener(new FActionMonitor());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
class FActionMonitor implements ActionListener
{
content.setText(content.getText+dis.readUTF());
}
class ActionMonitor implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
dos.writeUTF(sendText.getText());
content.setText(content.getText()+sendText.getText());
}
}
}

客户端:

import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class TalkCleint
{
public static void main(String[] args)
{
new MyFrame1("Client Talking...");
}
}
class MyFrame1 extends Frame
{
TextField content,sendText;
Button send;
Panel p;
Socket server=new ServerSocket("127.0.0.1",8887);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
MyFrame1(String title)
{
super(title);
content=new TextField();
sendText=new TextField();
send=new Button("发送");
send.addActionListener(new ActionMonitor());
p=new Panel(new GridLayout(1,2));
add(content,"North");
p.add(sendText);
p.add(send);
add(p,"South");
pack();
addActionListener(new FActionMonitor());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
}
class FActionMonitor implements ActionListener
{
String s=content.getText();
content.setText(s+dis.readUTF());
}
class ActionMonitor implements ActionListener
{

public void actionPerformed(ActionEvent e)
{
dos.writeUTF(sendText.getText());
String s=content.getText();
content.setText(s+(sendText.getText()));
}
}
}

最佳答案
  • 五星知识达人网友:旧脸谱
  • 2021-04-19 17:09

首先我可以说你对面向对象不够理解,对实例的访问理解不深,其次实现 ActionListener这个我就不说了,你是以为写了这句话,就可以监听了是吧,呵呵


如果你有需要,我再发我的代码给你,但我不建议

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯