📄 chatroom.java
字号:
/*
* ChatRoom.java
*
* Created on 2007年10月28日, 下午1:52
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package chatroomprogram;
/*
* ChatRoom.java
*
* Created on 2007年10月27日, 下午12:46
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.net.*;
import java.io.*;
public class ChatRoom extends JFrame
{
private JScrollPane sp1,sp2;
private JTextArea ta,ta1;
private JTextField txt1,txt2;
private JPanel p1,p2,p3;
private JButton bt1,bt2,bt3;
private String words,words1,word2;//保存输入框内容
private boolean t=false;//用来控制输出
int i=0,k=0;
Socket socket=null;
BufferedReader br=null;
PrintWriter pw=null;
public ChatRoom(){
super("ChatRoom");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con=getContentPane();
p1=new JPanel();
bt1=new JButton("Send");
bt2=new JButton("Close");
bt3=new JButton("longin");
p1.add(bt1);
p1.add(bt2);
p1.add(bt3);
p2=new JPanel();
ta=new JTextArea(10,40);
ta.setEditable(false);
sp1=new JScrollPane(ta);
p3=new JPanel();
ta1=new JTextArea(10,10);
ta1.setEditable(false);
sp2=new JScrollPane(ta1);
txt1=new JTextField();
txt2=new JTextField();
con.add(p1,BorderLayout.EAST);
con.add(sp1,BorderLayout.WEST);
con.add(sp2,BorderLayout.CENTER);
con.add(txt1,BorderLayout.SOUTH);
con.add(txt2,BorderLayout.NORTH);
this.pack();
setVisible(true);
ActionListener listener1=new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JButton bt=(JButton)e.getSource();
try{
if(bt==bt2){
if(socket != null)socket.close();
System.exit(0);}
}catch(Exception ee){ee.printStackTrace();}
}
};
bt2.addActionListener(listener1);
try{
socket = new Socket("localhost", 3000);
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
OutputStream os = socket.getOutputStream();
pw = new PrintWriter(os, true);
ActionListener listener2=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1||e.getSource()==txt1){
words1=txt2.getText();
words=txt1.getText();
pw.println(word2+"----speaking: "+words);
txt1.setText("");
}
}
};
bt1.addActionListener(listener2);
txt2.addActionListener(listener2);
ActionListener listener3=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt3||e.getSource()==txt2){
words1=txt2.getText();word2=words1;
pw.println(words1+" is online ");
txt2.setText("");
k=i;
i++;
}
}
};
bt3.addActionListener(listener3);
txt2.addActionListener(listener3);
ta.setText(ta.getText()+"第一行用来输入用户名中间的两个方框表示聊天内容和在线人最后一行输入你要说的内容" );
while(true){
// ta1.setText(ta1.getText()+"第一行用来输入用户名中间的两个方框表示聊天内容和在线人最后一行输入你要说的内容" );
String data = br.readLine();
if(i!=k){
ta1.setText(ta1.getText()+data+"\n");k++;}
else {
ta.setText(ta.getText()+"\n"+data);
}
}
/* while(true){
String data = br.readLine();
ta.setText(ta.getText()+"\n"+data);
}*/
}catch(Exception e){
e.printStackTrace();
}finally{
try{ if(pw !=null)pw.close();
if(br != null)br.close();
if(socket != null)socket.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
public static void main(String args[]){
new ChatRoom();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -