怎么发送不了消息啊

chenchangfeng1 2013-01-22 01:07:55
package gui1;


import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;


public class ChatAWT extends javax.swing.JFrame {
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
public JTextArea jTextArea2;
public JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JScrollPane jScrollPane2;
private Socket sk; //Socket对象
private String Text=""; //接收聊天记录
private BufferedReader bs;
private PrintWriter pw;
public ChatAWT(Socket sk) throws IOException
{
this.sk=sk;
pw=new PrintWriter(new OutputStreamWriter(sk.getOutputStream()));
}

public void initGUI(){
try {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().setBackground(new java.awt.Color(83,169,255));

jLabel1 = new JLabel();
getContentPane().add(jLabel1);
jLabel1.setText("我的聊天室");
jLabel1.setBounds(118, 12, 106, 26);
jLabel1.setFont(new java.awt.Font("新宋体",1,20));


jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1, "Center");
jScrollPane1.setBounds(45, 44, 262, 149);

jTextArea1 = new JTextArea();
jScrollPane1.setViewportView(jTextArea1);
jTextArea1.setBackground(new java.awt.Color(191,251,214));
jTextArea1.setEditable(false);



jScrollPane2 = new JScrollPane();
getContentPane().add(jScrollPane2, "Center");
jScrollPane2.setBounds(45, 215, 262, 109);

jTextArea2 = new JTextArea();
jScrollPane2.setViewportView(jTextArea2);



jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText("发送");
jButton1.setBounds(245, 330, 62, 24);
jButton1.addActionListener(action);


jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("清屏");
jButton2.setBounds(351, 169, 92, 24);
jButton2.addActionListener(action);


jButton3 = new JButton();
getContentPane().add(jButton3);
jButton3.setText("聊天记录");
jButton3.setBounds(351, 206, 92, 24);
jButton3.addActionListener(action);

jLabel2 = new JLabel();
getContentPane().add(jLabel2);
jLabel2.setText("客户B");
jLabel2.setBounds(339, 62, 112, 73);
jLabel2.setFont(new java.awt.Font("华文行楷",1,36));


jLabel3 = new JLabel();
getContentPane().add(jLabel3);
jLabel3.setText("客户A");
jLabel3.setFont(new java.awt.Font("华文行楷",1,36));
jLabel3.setBounds(339, 257, 112, 73);

pack();
this.setSize(500, 400);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setResizable(false);
} catch (Exception e) {
e.printStackTrace();}


}
ActionListener action =new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String string=e.getActionCommand();
if(string.equals("发送"))
{
try {
// BufferedReader bs=new BufferedReader(new InputStreamReader());
// PrintWriter pw=new PrintWriter(new OutputStreamWriter(sk.getOutputStream()));
// while((Text=bs.readLine())!=null)
pw.println(jTextArea2.getText());
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}


}
if(string.equals("清屏"))
{
jTextArea2.setText("");
}
if(string.equals("聊天记录"));
}

};

}


package gui1;

import java.io.IOException;
import java.net.Socket;

public class Chat {
public static void main(String[] args )throws IOException
{
Socket s=new Socket("localhost",30000);
ChatAWT chatAWT=new ChatAWT(s);
chatAWT.initGUI();
}

}服务器那段是对的 为什么服务器收不到消息,socket连接成功但是发不出消息
...全文
164 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenchangfeng1 2013-01-23
  • 打赏
  • 举报
回复
引用 2 楼 lwb314 的回复:
pw.println(jTextArea2.getText()); pw.flush();
发中文是时候服务器乱码
nmyangym 2013-01-23
  • 打赏
  • 举报
回复
我自己测试了一下,似乎与这没关系。咳!楼主再看看吧!
nmyangym 2013-01-23
  • 打赏
  • 举报
回复
引用 3 楼 chenchangfeng1 的回复:
引用 2 楼 lwb314 的回复:pw.println(jTextArea2.getText()); pw.flush();发中文是时候服务器乱码
乱码的话,把这句:
 pw=new PrintWriter(new OutputStreamWriter(sk.getOutputStream()));
用:
 pw=new PrintWriter(sk.getOutputStream());
试试看.
微风飘过 2013-01-23
  • 打赏
  • 举报
回复
加上这句 pw.flush();刷新缓冲
  • 打赏
  • 举报
回复
pw.println(jTextArea2.getText()); pw.flush();
  • 打赏
  • 举报
回复
pw.flush();//这句加上就应该好使了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧