江湖救急!我写了一个java聊天室程序,运行时可以打开聊天窗口,但无法发送消息,怎么会这样,求大神拯救 5
若回答被采纳,除财富值之外,另有重谢!!!/*客户端*/importjavax.swing.JFrame;importjavax.swing.JLabel;importj...
若回答被采纳,除财富值之外,另有重谢!!!
/*客户端*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class chatclient1 extends JFrame implements ActionListener
{
JLabel jl = new JLabel("输入");
JPanel jp = new JPanel();
JTextField jtf = new JTextField(15);
JTextArea jta = new JTextArea(10,10);
Socket sk=null;
BufferedReader br;
BufferedWriter bof;
PrintWriter pw;
public chatclient1()
{
super("用户端");
Container con = getContentPane();
jp.add(jl);
jp.add(jtf);
jta.setFont(new Font("标楷体", Font.BOLD, 16));
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setBackground(Color.green);
con.add(jp,BorderLayout.SOUTH);
con.add(jta,BorderLayout.CENTER);
jtf.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(400,280);
show();
try
{
Socket sk = new Socket("127.0.0.1",11);
jta.append("已连接到服务器"+sk.getInetAddress().getHostName()+"\n");
InputStream in=sk.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
pw = new PrintWriter(sk.getOutputStream(),true);
while(true)
{
String getString=br.readLine();
if(getString!=null){
jta.append("服务器:"+getString+"\n\n");
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try {
sk.close();
br.close();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent arg0)
{
try {
String str = jtf.getText();
jtf.setText("");
pw.println(str);
pw.flush();
jta.append("用户:"+str+"\n\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
new chatclient1();
}
}
public class chatserver1 extends JFrame implements ActionListener
{
JLabel jl = new JLabel("输入");
JPanel jp = new JPanel();
JTextField jtf = new JTextField(15);
JTextArea jta = new JTextArea(10,10);
ServerSocket ss;
Socket sk;
BufferedReader br;
PrintWriter pw; 展开
/*客户端*/
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
public class chatclient1 extends JFrame implements ActionListener
{
JLabel jl = new JLabel("输入");
JPanel jp = new JPanel();
JTextField jtf = new JTextField(15);
JTextArea jta = new JTextArea(10,10);
Socket sk=null;
BufferedReader br;
BufferedWriter bof;
PrintWriter pw;
public chatclient1()
{
super("用户端");
Container con = getContentPane();
jp.add(jl);
jp.add(jtf);
jta.setFont(new Font("标楷体", Font.BOLD, 16));
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setBackground(Color.green);
con.add(jp,BorderLayout.SOUTH);
con.add(jta,BorderLayout.CENTER);
jtf.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
setSize(400,280);
show();
try
{
Socket sk = new Socket("127.0.0.1",11);
jta.append("已连接到服务器"+sk.getInetAddress().getHostName()+"\n");
InputStream in=sk.getInputStream();
br = new BufferedReader(new InputStreamReader(in));
pw = new PrintWriter(sk.getOutputStream(),true);
while(true)
{
String getString=br.readLine();
if(getString!=null){
jta.append("服务器:"+getString+"\n\n");
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try {
sk.close();
br.close();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent arg0)
{
try {
String str = jtf.getText();
jtf.setText("");
pw.println(str);
pw.flush();
jta.append("用户:"+str+"\n\n");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
new chatclient1();
}
}
public class chatserver1 extends JFrame implements ActionListener
{
JLabel jl = new JLabel("输入");
JPanel jp = new JPanel();
JTextField jtf = new JTextField(15);
JTextArea jta = new JTextArea(10,10);
ServerSocket ss;
Socket sk;
BufferedReader br;
PrintWriter pw; 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询