聊天工具小程序
来源:互联网 发布:手机号群发短信软件 编辑:程序博客网 时间:2024/05/16 12:09
注:小实验,非完成的作品.
SERVER端:
import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;import java.net.*;public class TextEditor {JFrame ft=new JFrame("TextEditor");TextArea ta=new TextArea("",80,60);JTextField tf=new JTextField();BorderLayout bl=new BorderLayout(20,20);//ft.setSize(800,600);JMenuBar mb=new JMenuBar();JMenu m=new JMenu("Menu");JMenuItem mis=new JMenuItem("Save");JMenuItem mie=new JMenuItem("Exit");String str="Great";BufferedReader in;PrintWriter out;void init() {ft.setLayout(bl);Jiant jt=new Jiant(this);Record r=new Record(this);ft.setSize(800,600);mis.addActionListener(jt);mie.addActionListener(jt);ta.addKeyListener(r);tf.setActionCommand("TF");tf.addActionListener(jt);//ta.setAutoscrolls(true);m.add(mis);m.add(mie);mb.add(m);ft.add(ta,BorderLayout.CENTER);ft.add(tf,BorderLayout.SOUTH);ft.addWindowListener(r);ft.setJMenuBar(mb);ft.setVisible(true);}public static void main(String args[]) {//TextEditor te=new TextEditor();TextEditor te=new TextEditor();te.init();try {ServerSocket ss=new ServerSocket(7777);Socket client=ss.accept();te.in=new BufferedReader(new InputStreamReader(client.getInputStream()));te.out=new PrintWriter(client.getOutputStream(),true);while(true) {te.ta.append("Client says: "+te.in.readLine()+"\n");}} catch (Exception e) {}}}class Jiant implements ActionListener {TextEditor te;Jiant(TextEditor tec) {te=tec;}public void actionPerformed(ActionEvent e) {if((e.getActionCommand()).equals("Save")) {//Listen to Save//te.ta.insert("this is a great test",0);try {FileDialog fd=new FileDialog(te.ft,"Select or New file",FileDialog.SAVE);fd.setVisible(true);String fn=fd.getDirectory()+fd.getFile();System.out.println(fn);te.str=te.ta.getText();PrintWriter pw=new PrintWriter(new FileOutputStream(fn,true));pw.print(te.str); pw.close();} catch (Exception en) {}}if((e.getActionCommand()).equals("Exit")) {//Listen to Exit//te.in.close();//te.out.close();te.ta.insert(te.str,0);}if((e.getActionCommand()).equals("TF")) { //output to client------te.ta.append(te.tf.getText()+"\n");te.out.println(te.tf.getText());te.tf.setText("");}}}class Record implements KeyListener ,WindowListener{TextEditor te;Record(TextEditor tec) {te=tec;}public void keyPressed(KeyEvent e) {}public void keyReleased(KeyEvent e) {}public void keyTyped(KeyEvent e) {//te.str+=e.getKeyChar();}public void windowClosing(WindowEvent e) {System.exit(0);}public void windowClosed(WindowEvent e){}public void windowIconified(WindowEvent e) {}public void windowDeiconified(WindowEvent e){}public void windowActivated(WindowEvent e){}public void windowDeactivated(WindowEvent e){}public void windowOpened(WindowEvent e) {}}
CLIENT端:
import java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;class NewClient extends Frame implements ActionListener{BorderLayout bl=new BorderLayout(20,20);TextArea ta=new TextArea();TextField tf=new TextField();private Socket client;String serverAddress = "127.0.0.1";int port = 7777;BufferedReader in;PrintWriter out;NewClient(){ try{ setLayout(bl); add(ta,BorderLayout.CENTER); add(tf,BorderLayout.SOUTH); tf.addActionListener(this); setVisible(true); client = new Socket(serverAddress,port);String check; // BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));in=new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(),true);while(true) {//check=sin.readLine();// out.println("Client says: "+sin.readLine() + "/n");check=in.readLine();if(check.equals("exit")) break; ta.append("Message from server: " + check+"\n"); } in.close(); out.close(); client.close(); }catch(Exception e){ e.printStackTrace(); }}public static void main(String[] args){ new NewClient();}public void actionPerformed(ActionEvent e) {String str=tf.getText();ta.append(str+"\n");out.println(str);tf.setText("");}}
收获:之前都是用命令行写server端和client端,只能实现你说一句我说一句的阻塞式交流.
今天的聊天工具加入了图形界面.同时在main函数中,让输入端一直从管道监听,对方只要有输入就写到TEXTAREA中去. 然后用时间监听
TEXTFIELD,巧回车就将textField中的数据写出. 这样将输入输出分开在不同模块...使输入输出不再相互阻塞...这个就实现了我们像QQ一样的聊天.
- 聊天工具小程序
- 聊天工具
- 聊天工具
- Node教学--02 TCP服务器(小聊天工具)
- 聊聊 聊天工具
- 局域网聊天工具
- 办公室聊天工具
- Web聊天工具
- 局域网聊天工具
- 局域网聊天工具
- 局域网聊天工具
- UDP聊天工具
- 即时聊天工具
- 网络聊天工具
- 聊天工具 客户端
- 聊天工具服务器
- 蓝牙聊天工具
- 局域网聊天工具
- 随笔二十三:为什么要学习机器代码?
- textarea框回車轉換行
- 动态调整对话框窗口大小的窗口类的资源链接
- 创建型模式之抽象工厂模式(AbstractFactory Pattern)C++实现******
- 开发Android主攻四大方向
- 聊天工具小程序
- ffmpeg-0.10.2 xcode4.3 IOS5.1 真机调试编译过程
- 基于libnids的TCP数据流的还原(多线程实现)
- 黑客文化 何处的中国计算机文化(二)
- ASP.NET中的HttpWorkerRequest对像及其应用
- android系统触摸屏虚拟按键
- 给字符数组赋值的方法
- 随笔二十四:逆向工程
- 人生关注的另一面