81,091
社区成员
发帖
与我相关
我的任务
分享
package hHChat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
class ServerGUI extends JPanel implements Runnable{
//按钮
private JButton export;//导出聊天记录
private JButton view;//查看聊天记录
private JButton send;//发送
private JButton exit;//退出
private JButton back;//返回
//文本
private JTextArea output;//显示框
private JTextField input;//输入框
private String s=" "+"\n\r";
//聊天记录
private JTextArea record;
File file=new File("聊天记录.txt");
ServerSocket SSocket;
Socket socket;
int port;
DataOutputStream outStream;
DataInputStream inStream;
public static void main(String[] args){
//创建窗口
JFrame fra=new JFrame("Ann's ChatRoom S");
fra.setContentPane(new ServerGUI());
fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fra.pack();
fra.setSize(800,494);
fra.setLocationRelativeTo(null);
fra.setVisible(true);
ServerGUI sg=new ServerGUI();
sg.address();
}
public ServerGUI(){
//创建原件
export=new JButton("导出");
view=new JButton("聊天记录");
send=new JButton("发送");
exit=new JButton("退出");
back=new JButton("返回");
output=new JTextArea();
input=new JTextField();
record=new JTextArea();
//添加事件
export.addActionListener(new Export());
view.addActionListener(new View());
send.addActionListener(new Send());
exit.addActionListener(new Exit());
back.addActionListener(new Back());
//布局
setLayout(null);
add(export);
export.setBounds(640, 20, 100, 30);
add(view);
view.setBounds(640, 128, 100, 30);
add(send);
send.setBounds(640, 232, 100, 30);
add(exit);
exit.setBounds(640,346,100,30);
add(back);
back.setBounds(640, 232, 100, 30);
back.setVisible(false);
add(output);
output.setBounds(40, 10, 494, 217);
output.setEditable(false);
add(input);
input.setBounds(40,230,494,217);
add(record);
record.setBounds(40, 10, 494, 424);
record.setEditable(false);
record.setVisible(false);
}
public void address(){
try {
SSocket=new ServerSocket(8888);
System.out.println("Server is created and waiting Client to connect...");
socket=SSocket.accept();
output.setText("客户端IP:"+socket.getInetAddress().getHostAddress());
outStream=new DataOutputStream(socket.getOutputStream());
inStream=new DataInputStream(socket.getInputStream());
new Thread(this).start();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
class Exit implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class Back implements ActionListener{
public void actionPerformed(ActionEvent e) {
output.setVisible(true);
input.setVisible(true);
send.setVisible(true);
view.setVisible(true);
export.setVisible(true);
record.setVisible(false);
back.setVisible(false);
}
}
class Export implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
try {
PrintWriter out=
new PrintWriter(new FileWriter(file));
out.print(output.getText());
out.close();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
class View implements ActionListener{
public void actionPerformed(ActionEvent e) {
output.setVisible(false);
input.setVisible(false);
send.setVisible(false);
view.setVisible(false);
export.setVisible(false);
record.setVisible(true);
back.setVisible(true);
try {
BufferedReader read=
new BufferedReader(new FileReader(file));
String line=read.readLine();
while(line!=null){
record.append(line+"\n\r");
line=read.readLine();
}
read.close();
} catch (FileNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
class Send implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
//读取input输入框输入的数据并以网络输出流outStream将数据送入网络而输出
outStream.writeUTF("服务器:"+input.getText());
output.setText(input.getText());
/*
* 读取input输入框输入的数据,记录在output显示框中
* s保存之前的对话,使对话能够连续显示
*/
String sentence=s;
while(input.getText()!=null){
output.setText(sentence+"服务器:"+input.getText()+"\n\r");
input.setText(null);//清空输入框
break;
}
s=output.getText();
} catch (IOException e1) {
output.setText("网络连接中断!");
}
}
}
public void run() {
while(true){
String netTransferLine;
try {
netTransferLine = inStream.readUTF();
output.setText(netTransferLine);
} catch (IOException e) {
output.setText("网络连接中断");
}
}
}
}
package hHChat;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
class ClientGUI extends JPanel implements Runnable{
//按钮
private JButton export;//导出聊天记录
private JButton view;//查看聊天记录
private JButton send;//发送
private JButton exit;//退出
private JButton back;//返回
//文本
private JTextArea output;//显示框
private JTextField input;//输入框
private String s=" "+"\n\r";
//聊天记录
private JTextArea record;
File file=new File("聊天记录.txt");
Socket socket;
static String iaddr;
static int port;
DataOutputStream outStream;
DataInputStream inStream;
public static void main(String[] args){
JFrame fra=new JFrame("Ann's ChatRoom C");
fra.setContentPane(new ClientGUI());
fra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fra.pack();
fra.setSize(800,494);
fra.setLocationRelativeTo(null);
fra.setVisible(true);
ClientGUI cg=new ClientGUI();
cg.address();
}
public ClientGUI(){
//创建原件
export=new JButton("导出");
view=new JButton("聊天记录");
send=new JButton("发送");
exit=new JButton("退出");
back=new JButton("返回");
output=new JTextArea();
input=new JTextField();
record=new JTextArea();
//添加事件
export.addActionListener(new Export());
view.addActionListener(new View());
send.addActionListener(new Send());
exit.addActionListener(new Exit());
back.addActionListener(new Back());
//布局
setLayout(null);
add(export);
export.setBounds(640, 20, 100, 30);
add(view);
view.setBounds(640, 128, 100, 30);
add(send);
send.setBounds(640, 232, 100, 30);
add(exit);
exit.setBounds(640,346,100,30);
add(back);
back.setBounds(640, 232, 100, 30);
back.setVisible(false);
add(output);
output.setBounds(40, 10, 494, 217);
output.setEditable(false);
add(input);
input.setBounds(40,230,494,217);
add(record);
record.setBounds(40, 10, 494, 424);
record.setEditable(false);
record.setVisible(false);
}
public void address(){
try {
InetAddress address = InetAddress.getLocalHost();
String a=address.getHostAddress();
socket=new Socket(a,8888);
outStream=new DataOutputStream(socket.getOutputStream());
inStream=new DataInputStream(socket.getInputStream());
new Thread(this).start();
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void run() {
while(true){
try {
//读取从网络流inStream传送来的数据,存放在netTransferLine中
String netTransferLine=inStream.readUTF();
//将netTransferLine中的数据记录在output中
output.setText(netTransferLine);
} catch (IOException e) {
// TODO 自动生成的 catch 块
output.setText("网络连接断开!");
}
}
}
class Exit implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class Back implements ActionListener{
public void actionPerformed(ActionEvent e) {
output.setVisible(true);
input.setVisible(true);
send.setVisible(true);
view.setVisible(true);
export.setVisible(true);
record.setVisible(false);
back.setVisible(false);
}
}
class Export implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
try {
PrintWriter out=
new PrintWriter(new FileWriter(file));
out.print(output.getText());
out.close();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
class View implements ActionListener{
public void actionPerformed(ActionEvent e) {
output.setVisible(false);
input.setVisible(false);
send.setVisible(false);
view.setVisible(false);
export.setVisible(false);
record.setVisible(true);
back.setVisible(true);
try {
BufferedReader read=
new BufferedReader(new FileReader(file));
String line=read.readLine();
while(line!=null){
record.append(line+"\n\r");
line=read.readLine();
}
read.close();
} catch (FileNotFoundException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
} catch (IOException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
}
class Send implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
//读取input输入框输入的数据并以网络输出流outStream将数据送入网络而输出
outStream.writeUTF("客户端:"+input.getText());
output.setText(input.getText());
/*
* 读取input输入框输入的数据,记录在output显示框中
* s保存之前的对话,使对话能够连续显示
*/
String sentence=s;
while(input.getText()!=null){
output.setText(sentence+"服务器:"+input.getText()+"\n\r");
input.setText(null);//清空输入框
break;
}
s=output.getText();
} catch (IOException e1) {
output.setText("网络连接中断!");
}
}
}
}