使用socket实现两台电脑通讯的问题 本帖最后由 caoxulei 于 2010-12-04 23:30:25 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是界面以及处理的类package com.caoxulei;import javax.swing.*;import java.io.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;public class ChatJFrame extends JFrame implements ActionListener { private JTextArea text_receiver; private JTextField text_sender; private PrintWriter cout; private String name;public ChatJFrame(String name, String title,PrintWriter cout) { // TODO 自动生成构造函数存根 super("聊天室"+name +" "+title); this.setSize(320, 240); this.setLocation(300, 240); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.text_receiver= new JTextArea(); this.text_receiver.setEditable(false); this.add(this.text_receiver); JPanel panel=new JPanel(); this.add(panel,"South"); this.text_sender=new JTextField(12); panel.add(this.text_sender); this.text_sender.addActionListener(this); JButton button_send=new JButton("发送"); panel.add(button_send); button_send.addActionListener(this); JButton button_leava =new JButton("离线"); panel.add(button_leava); button_leava.addActionListener(this); this.setVisible(true); this.setWriter(cout); this.name=name; } public void setWriter(PrintWriter cout) { this.cout=cout; } public void receive(String message) { text_receiver.append(message+"\r\n"); } public void actionPerformed(ActionEvent e) { // TODO 自动生成方法存根 if (e.getActionCommand()=="离线") { if (this.cout!=null){ this.cout.println(name+"离线"); this.cout.print("bye"); this.cout=null; } text_receiver.append("我离线\n"); } else{ if (this.cout!=null){ this.cout.println(name+"说:"+text_sender.getText()); text_receiver.append("我说:"+text_sender.getText()+"\n"); text_sender.setText(""); } else text_receiver.append("已离线,不能发送信息。\n"); } }} 再学习一下NAT打洞技术,就好用了 第一个hibernate 程序找不到数据库驱动 一個取串的問題!有點急! java程序异常造成window系统重启 怎样分别调度两个进程 请问SWING的UI类如何使用,最好有例子。 50分 很基础的问题。进来留言都有分 请过来人指教! JAVA对象转为Java String的几种常用方法 JTree中如何改变节点的背景颜色? 如何存储这个结构? 求助,JTable对象建立好后还能使用getToolTipText(MouseEvent event)吗? 新手的问题,在界面上画一个月亮,通过按钮来改变月亮的颜色
import javax.swing.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;public class ChatJFrame extends JFrame implements ActionListener {
private JTextArea text_receiver;
private JTextField text_sender;
private PrintWriter cout;
private String name;
public ChatJFrame(String name, String title,PrintWriter cout) {
// TODO 自动生成构造函数存根
super("聊天室"+name +" "+title);
this.setSize(320, 240);
this.setLocation(300, 240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.text_receiver= new JTextArea();
this.text_receiver.setEditable(false);
this.add(this.text_receiver);
JPanel panel=new JPanel();
this.add(panel,"South");
this.text_sender=new JTextField(12);
panel.add(this.text_sender);
this.text_sender.addActionListener(this);
JButton button_send=new JButton("发送");
panel.add(button_send);
button_send.addActionListener(this);
JButton button_leava =new JButton("离线");
panel.add(button_leava);
button_leava.addActionListener(this);
this.setVisible(true);
this.setWriter(cout);
this.name=name;
}
public void setWriter(PrintWriter cout) {
this.cout=cout;
}
public void receive(String message) {
text_receiver.append(message+"\r\n");
}
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
if (e.getActionCommand()=="离线")
{
if (this.cout!=null){
this.cout.println(name+"离线");
this.cout.print("bye");
this.cout=null;
}
text_receiver.append("我离线\n");
}
else{
if (this.cout!=null){
this.cout.println(name+"说:"+text_sender.getText());
text_receiver.append("我说:"+text_sender.getText()+"\n");
text_sender.setText("");
}
else
text_receiver.append("已离线,不能发送信息。\n");
}
}
}