import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
/**
* 用于定义并启动对话框窗体。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
public class ChatFrame extends JFrame
{
public ChatFrame(String name,String id,String mobile,Socket socket) throws IOException
{
this.name = name;
this.socket = socket;
this.id = id;
this.mobile = mobile;
System.out.println("Connection to Server");
setTitle("多人聊天系统 客户端 v01.00 --->>>tted<<<--- 用户:" + name );
setBounds(300,200,DEFAULT_WIDTH,DEFAULT_HEIGHT);
setBackground(Color.cyan);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
Border etched = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(etched,INPUT);
Border infoBorder = BorderFactory.createTitledBorder(etched,INFO);
Border messageBorder = BorderFactory.createTitledBorder(etched,MESSAGE);
messageArea = new JTextArea(14,20);
messageArea.setBorder(messageBorder);
messageArea.setEditable(false);
scrollPane = new JScrollPane(messageArea);
inputArea = new JTextArea(6,20);
inputArea.setBorder(border);
inputArea.setColumns(20);
inputScrollPane = new JScrollPane(inputArea);
infoArea = new JTextArea(8,20);
infoArea.setBorder(infoBorder);
infoArea.setEditable(false);
buttonSend = new Button("Send");
buttonCancel = new Button("Cancel");
buttonClear = new Button ("Clear");
buttonExit = new Button ("Exit");
buttonHistory = new Button("History");
buttonSend.addActionListener(new InputListener());
buttonCancel.addActionListener(new InputListener());
buttonClear.addActionListener(new InputListener());
buttonExit.addActionListener(new InputListener());
buttonHistory.addActionListener(new InputListener());
JPanel inputPanel = new JPanel();
inputPanel.setBackground(Color.GRAY);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.GRAY);
buttonPanel.add(buttonSend);
buttonPanel.add(buttonCancel);
buttonPanel.add(buttonClear);
buttonPanel.add(buttonHistory);
buttonPanel.add(buttonExit);
inputPanel.add(infoArea);
inputPanel.add(inputScrollPane);
inputPanel.add(buttonPanel);
this.add(scrollPane,BorderLayout.CENTER);
this.add(inputPanel,BorderLayout.SOUTH);
infoArea.setText("name: " + name + "\n" + "id: " + id + "\n" + "mobile: " + mobile);
new InputThread().start();
this.addWindowListener(new WindowAdapter());
}
/**
* 用于自定义窗口监听器类(实现退出时关闭相应流与端口)。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
class WindowAdapter implements WindowListener
{
public void windowClosing(WindowEvent e) {
try
{
os = new PrintWriter(socket.getOutputStream());
os.println(message);
os.flush();
fos = new FileOutputStream(file,true);
fpw = new PrintWriter(fos);
fpw.println(message);
fpw.flush();
socket.close();
System.out.println(message);
inputArea.setText("");
}
catch(IOException e1)
{
e1.getStackTrace();
}
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
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) {}
}
/**
* 用于定义一个输入线程。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
class InputThread extends Thread
{
public void run()
{
while(true)
{
try
{
BufferedReader is = new BufferedReader (new InputStreamReader(socket.getInputStream()));
receiveMessage = is.readLine();
}
catch(IOException e)
{
e.getStackTrace();
}
messageArea.append(receiveMessage + "\n");
}
}
}
/**
* 用于定义一个输出线程。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
class OutputThread extends Thread
{
public void run()
{
matcher = patternOS.matcher(inputArea.getText());
if(matcher.find())
message = name + ":请文明发言";
else
message = name + ":"+ inputArea.getText();
try
{
os = new PrintWriter(socket.getOutputStream());
os.println(message);
os.flush();
fos = new FileOutputStream(file,true);
fpw = new PrintWriter(fos);
fpw.println(message);
fpw.flush();
System.out.println(message);
inputArea.setText("");
}
catch(IOException e)
{
e.getStackTrace();
}
}
}
/**
* 用于定义一个按钮监听器(实现面板中各按钮功能)。
*
* @version 01.00 2012-8-28 guzhaocheng
* @author gu zhaocheng
*/
class InputListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == buttonSend)
{
new OutputThread().start();
}
if(e.getSource() == buttonCancel)
{
inputArea.setText("");
}
if(e.getSource() == buttonClear)
{
messageArea.setText("");
}
if(e.getSource() == buttonExit)
{
try
{
socket.close();
System.exit(0);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
if(e.getSource() == buttonHistory)
{
try
{
Runtime.getRuntime().exec(cmd);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) throws UnknownHostException, IOException
{
EventQueue.invokeLater(new Runnable(){
public void run()
{
try
{
new ChatFrame("guest","---","---",new Socket(InetAddress.getLocalHost(),4800));
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
}
String cmd="rundll32 url.dll FileProtocolHandler file://f:/file.txt ";
File file = new File("F:\\","file.txt");
String regex = "[asd]\\s[fg|rt][a-zA-Z]";
Pattern patternOS = Pattern.compile(regex);
Matcher matcher;
private static final long serialVersionUID = -5357073211248352588L;
static final int DEFAULT_WIDTH = 800;
static final int DEFAULT_HEIGHT = 400;
static final String MESSAGE = "聊天内容";
static final String INFO = "用户信息";
static final String INPUT = "在此输入内容";
Button buttonSend;
Button buttonCancel;
Button buttonClear;
Button buttonExit;
Button buttonHistory;
JScrollPane scrollPane;
JScrollPane inputScrollPane;
String name;
String id;
String mobile;
Socket socket;
PrintWriter os;
FileOutputStream fos;
PrintWriter fpw;
String message;
String receiveMessage;
JTextArea messageArea;
JTextArea infoArea;
JTextArea inputArea;
}
java聊天室 提交预版 对话框
最新推荐文章于 2023-04-16 17:35:20 发布