//???§??
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.net.UnknownHostException; import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException; import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants;
public class ChatRoom
{
public static void main(String[] args)
{
ChatRoomFrame f = new ChatRoomFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true);
}
}
class ChatRoomFrame extends JFrame
{
public ChatRoomFrame()
{
setTitle("????");
setBounds(300, 200, 200, 250);
setResizable(false);
ChatRoomPanel p = new ChatRoomPanel(ChatRoomFrame.this); add(p);
}
}
class ChatRoomPanel extends JPanel
{
private JLabel lname, ladr, lcom;
private JTextField tname, tadr, tcom;
private JButton log, reset;
private ChatRoomFrame frame1;
private Socket socket;
public ChatRoomPanel(ChatRoomFrame f)
{
frame1 = f;
lname = new JLabel("ê?????");
ladr = new JLabel("???·??");
lcom = new JLabel("??????");
tname = new JTextField(10);
tadr = new JTextField("127.0.0.1");
tcom = new JTextField("10000");
log = new JButton("????");
reset = new JButton("????");
add(lname);
add(tname);
add(ladr);
add(tadr);
add(lcom);
add(tcom);
add(log);
add(reset);
log.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if ("".equals(tname.getText().trim()))
{
JOptionPane.showMessageDialog(null,
"ê?????????????");
} else if ("".equals(tadr.getText().trim())
|| "".equals(tcom.getText().trim()))
{
JOptionPane.showMessageDialog(null,
"?????????·?ò??????");
} else
{
try
{
socket = new Socket(tadr.getText().trim(),Integer.parseInt(tcom.getText().trim()));
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
ChatInfo checkname = new ChatInfo(tname.getText().trim());
oos.writeObject(checkname);
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
MessageInfo checkmsg = (MessageInfo)ois.readObject();
if(checkmsg.isWhisper())
{
JOptionPane.showMessageDialog(null, checkmsg.getMsg());
}
else
{
frame1.setVisible(false);
ChatRoom1Frame f1 = new ChatRoom1Frame(tname, socket);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
} catch (NumberFormatException e)
{
e.printStackTrace();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
});
reset.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tname.setText("");
tname.requestFocus();
}
});
}
}
class ChatRoom1Frame extends JFrame
{
public ChatRoom1Frame(JTextField name, Socket socket)
{
setTitle("???ì??");
setBounds(200, 50, 850, 700);
ChatRoom1Panel p = new ChatRoom1Panel(ChatRoom1Frame.this, name,socket);
add(p);
}
}
class ChatRoom1Panel extends JSplitPane
{
private JTextArea msg;
private JTextPane pub, ind;
private JLabel lmsg, llist;
private JButton color, rename, send, close;
private JTextField newname;
private JCheckBox chatmode;
private JList list;
private JComboBox style, sendmode, size;
private Vector<String> namelist = new Vector<String>(); private boolean ctrl, enter;
private boolean whisper;
private String sname, netAddress;
private ChatRoom1Frame frame;
private Socket socket;
private JScrollPane sp1, sp2;
private ObjectOutputStream oos;
private ObjectInputStream ois;
private MessageInfo msginfo;
public ChatRoom1Panel(ChatRoom1Frame f, JTextField name, Socket socket)
{
sname = name.getText().trim();
frame = f;
this.socket = socket;
try
{
netAddress = socket.getInetAddress().toString();
ChatInfo info0 = new ChatInfo(sname, 0, false, null, null,
null,
false, netAddress);
oos = new ObjectOutputStream(socket.getOutputStream()); // ???????ó·????÷
oos.writeObject(info0);
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
setOrientation(JSplitPane.HORIZONTAL_SPLIT);
LeftPanel left = new LeftPanel();
RightPanel right = new RightPanel();
setLeftComponent(left);
setRightComponent(right);
setDividerLocation(610);
setDividerSize(5);
Connection con = new Connection(list);
con.start();
//
}
class LeftPanel extends JSplitPane
{
public LeftPanel()
{
setOrientation(JSplitPane.VERTICAL_SPLIT);
PubAndInd pai = new PubAndInd();
MsgAndTool mat = new MsgAndTool();
setDividerSize(5);
setDividerLocation(480);
setTopComponent(pai);
setBottomComponent(mat);
}
}
class RightPanel extends JPanel { public RightPanel() { BorderLayout bd = new BorderLayout(); setLayout(bd); llist = new JLabel(" ???§±í:"); list = new JList(namelist); JScrollPane sp = new JScrollPane(list); list.setVisibleRowCount(32); add(llist, "North"); add(sp, "South"); } } class PubAndInd extends JSplitPane { public PubAndInd() { setOrientation(JSplitPane.VERTICAL_SPLIT); setDividerSize(5); setDividerLocation(280); pub = new JTextPane(); ind = new JTextPane(); pub.setEditable(false); ind.setEditable(false); sp1 = new JScrollPane(pub); sp2 = new JScrollPane(ind); setTopComponent(sp1); setBottomComponent(sp2); } } class MsgAndTool extends JSplitPane { public MsgAndTool() { setOrientation(JSplitPane.VERTICAL_SPLIT);
setDividerSize(5); setDividerLocation(40); MsgPanel mp = new MsgPanel(); ToolPanel tp = new ToolPanel(); setTopComponent(tp); setBottomComponent(mp); } } class ToolPanel extends JPanel { public ToolPanel() { Vector<Integer> si = new Vector<Integer>(); for (int i = 0; i < 11; i++) { si.add(2 * i + 8); } setLayout(null); chatmode = new JCheckBox("????"); rename = new JButton("????:"); newname = new JTextField(6); newname.setText(sname); style = new JComboBox(GraphicsEnvironment .getLocalGraphicsEnvironment() .getAvailableFontFamilyNames()); style.setSelectedItem("????"); color = new JButton("????"); close = new JButton("????"); size = new JComboBox(si); size.setSelectedItem(12); chatmode.setBounds(5, 5, 60, 30); rename.setBounds(70, 5, 70, 30); newname.setBounds(150, 5, 120, 30); style.setBounds(280, 5, 120, 30); size.setBounds(405, 5, 40, 30); color.setBounds(445, 5, 70, 30); close.setBounds(520, 5, 70, 30); add(chatmode); add(rename);
add(newname);
add(style);
add(color);
add(close);
add(size);
color.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
Color c = JColorChooser.showDialog(frame, "????????",
Color.black);
msg.setForeground(c);
}
});
style.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED) {
msg.setFont(new Font(
style.getSelectedItem().toString(), msg .getFont().getStyle(),
msg.getFont()
.getSize()));
}
}
});
size.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange() == ItemEvent.SELECTED) {
msg.setFont(new Font(msg.getFont().getFamily() .toString(), msg.getFont().getStyle(), (Integer) size.getSelectedItem()));
}
}
});
chatmode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (chatmode.isSelected())
{
whisper = true;
} else
{
whisper = false;
}
}
});
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try
{
ChatInfo info = new ChatInfo(sname, list .getSelectedIndex() - 1, whisper, msg.getText(), msg.getFont(), msg .getForeground(),
netAddress);
oos =
ObjectOutputStream(socket.getOutputStream());
// ???????ó·????÷
oos.writeObject(info);
// oos.close();
// ois.close();
} catch (IOException e)
{
e.printStackTrace();
}
System.exit(0);
}
});
true, new
frame.addWindowListener(new WindowListener()
{
public void windowActivated(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0)
{
}
public void windowClosing(WindowEvent arg0)
{
close.doClick();
}
public void windowDeactivated(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowIconified(WindowEvent arg0) {
}
public void windowOpened(WindowEvent arg0)
{
}
});
rename.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
if ("".equals(newname.getText().trim()))
{
JOptionPane.showConfirmDialog(ChatRoom1Panel.this,
"ê?????????????", "?·??", JOptionPane.CLOSED_OPTION,
JOptionPane.ERROR_MESSAGE, null);
} else
{
sname = newname.getText();
JOptionPane.showConfirmDialog(ChatRoom1Panel.this,
"??????????", "?·??", JOptionPane.CLOSED_OPTION,
JOptionPane.INFORMATION_MESSAGE, null); }
}
});
}
}
class MsgPanel extends JPanel
{
private Vector<String> v = new Vector<String>();
public MsgPanel()
{
setLayout(null);
lmsg = new JLabel("??????");
send = new JButton("·???");
msg = new JTextArea(4, 30);
v.add("°? Enter ·???");
v.add("°? Ctrl+Enter ·???");
sendmode = new JComboBox(v);
sendmode.setSelectedIndex(0);
lmsg.setBounds(5, 5, 40, 20);
msg.setBounds(45, 0, 450, 120);
send.setBounds(500, 10, 70, 30);
sendmode.setBounds(500, 60, 100, 20);
setBackground(new Color(200, 200, 200));
msg.setLineWrap(true);
add(lmsg);
add(send);
add(msg);
add(sendmode);
msg.addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if (sendmode.getSelectedIndex() == 0) { if (code == KeyEvent.VK_ENTER) { e.consume(); send.doClick(); } } if (sendmode.getSelectedIndex() == 1) { if (code == KeyEvent.VK_CONTROL) { ctrl = true; } if (code == KeyEvent.VK_ENTER) { enter = true; } if (ctrl && enter) { e.consume(); send.doClick(); } } } public void keyReleased(KeyEvent e) { int co = e.getKeyCode(); if (co == KeyEvent.VK_CONTROL) { ctrl = false; }
if (co == KeyEvent.VK_ENTER)
{
enter = false;
}
}
public void keyTyped(KeyEvent e)
{
}
});
send.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0) {
ChatInfo info = new ChatInfo(sname,
list.getSelectedIndex(), whisper, msg.getText(),
msg.getFont(), msg.getForeground(), false, netAddress);
try
{
oos = new ObjectOutputStream(socket.getOutputStream());
// ???????ó·????÷
oos.writeObject(info);
msg.setText("");
msg.requestFocus();
} catch (UnknownHostException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}
public void insertDocument(String text, Color textColor, int size, String family, JTextPane textpane)// ?ù?????????????°??×???????×???????±??ò
{
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setForeground(set, textColor);// ?è????×????? StyleConstants.setFontSize(set, size);// ?è??×????ó?? StyleConstants.setFontFamily(set, family);
Document doc = textpane.getStyledDocument();
try
{
doc.insertString(doc.getLength(), text, set);// ??????×? } catch (BadLocationException e)
{
}
}
class Connection extends Thread
{
private JList li;
public Connection(JList li)
{
this.li = li;
}
public void run()
{
try
{
while (true)
{
ois = new ObjectInputStream(socket.getInputStream());
msginfo = (MessageInfo) ois.readObject();
if (msginfo.isExit())
{
break;
} else
{
namelist = msginfo.getNamelist();
list.setListData(namelist);
list.setSelectedIndex(msginfo.getIndex());
SwingUtilities.updateComponentTreeUI(li);
if (msginfo.isWhisper())
{
if (list.getSelectedIndex() >= 0)
{
Point p2 = new Point();
p2.setLocation(0, ind.getHeight()); sp2.getViewport().setViewPosition(p2);
insertDocument(msginfo.getNickname() + " "
+ "???????? "
+
list.getSelectedValue().toString()
+ " ??:", Color.black, 12, "????", ind);
insertDocument(msginfo.getMsg() + "\r\n", msginfo.getColor(),
msginfo.getFont()
.getSize(),
msginfo.getFont()
.getFamily(), ind); } else
{
insertDocument(msginfo.getNickname() + " "
+ "????????:", Color.black, 12, "????", ind);
insertDocument(msginfo.getMsg() + "\r\n", msginfo.getColor(),
msginfo.getFont()
.getSize(),
msginfo.getFont()
.getFamily(), ind); }
} else
{
if ("".equals(msginfo.getNickname())) {
insertDocument(msginfo.getMsg() + "\r\n", msginfo.getColor(),
msginfo.getFont()
.getSize(),
msginfo.getFont()
.getFamily(), pub); } else
{
Point p1 = new Point();
p1.setLocation(0, pub.getHeight()); sp1.getViewport().setViewPosition(p1);
insertDocument(msginfo.getNickname() + " "
+ "??:", Color.black, 12, "????", pub);
insertDocument(msginfo.getMsg() + "\r\n", msginfo.getColor(),
msginfo.getFont()
.getSize(),
msginfo.getFont()
.getFamily(), pub); }
}
}
}
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
class ChatInfo implements Serializable
{
private String nickname, message, netAddress;
private int index;
private boolean whisper;
private Font font;
private Color color;
private boolean exit;
public ChatInfo(String name, int in, boolean whi, String msg, Font fo,
Color colo, boolean ex, String netaddr)
{
nickname = name;
index = in;
whisper = whi;
message = msg;
font = fo;
color = colo;
netAddress = netaddr;
exit = ex;
}
public ChatInfo(String name)
{
nickname = name;
}
public String getNickname()
{
return nickname;
}
public String getMessage()
{
return message;
}
public int getIndex()
{
return index;
}
public boolean isWhisper() {
return whisper;
}
public Font getFont()
{
return font;
}
public Color getColor() {
return color;
}
public boolean isExit() {
return exit;
}
public String getNetAddress() {
return netAddress;
}
}
//·????÷??
import java.awt.Color;
import java.awt.Font;
import java.awt.Window;
import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.net.ServerSocket; import java.net.Socket;
import java.util.ArrayList; import java.util.Vector;
import javax.swing.JOptionPane;
class ChatRoomServer extends Thread
{
private ServerSocket server;
private ArrayList<Socket> list = new ArrayList<Socket>(); private Vector<String> namelist = new Vector<String>();
public ChatRoomServer()
{
try
{
server = new ServerSocket(10000);
} catch (IOException e)
{
e.printStackTrace();
System.out.println("·????÷?????§°?!");
}
}
public void run()
{
try
{
while (true)
{
Socket s = server.accept();
boolean same = false;
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
ChatInfo checkname = (ChatInfo)ois.readObject(); for(String na:namelist)
{
if(checkname.getNickname().equals(na))
{
same = true;
break;
}
}
if(same)
{
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
MessageInfo checkmsg = new MessageInfo("??ê???????????",true);
oos.writeObject(checkmsg);
s.close();
}
else
{
ObjectOutputStream oos = ObjectOutputStream(s.getOutputStream());
MessageInfo checkmsg = MessageInfo("",false);
oos.writeObject(checkmsg);
list.add(s);
ServerRunner sr = new ServerRunner(s, namelist);
sr.start();
// break;
}
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new ChatRoomServer().start();
}
}
class ServerRunner extends Thread
{
private Socket s;
private ArrayList<Socket> list;
private Vector<String> namelist;
private ChatInfo chatinfo;
private ObjectInputStream ois; new new list,
private ObjectOutputStream oos;
public ServerRunner(Socket s, ArrayList<Socket> l, Vector<String> namelist)
{
this.s = s;
list = l;
this.namelist = namelist;
}
public void run()
{
String socketName;
try
{
ois = new ObjectInputStream(s.getInputStream());
chatinfo = (ChatInfo) ois.readObject();
namelist.add(chatinfo.getNickname());
socketName = chatinfo.getNickname();
MessageInfo msginfo0 = new MessageInfo("", chatinfo.getNickname()
+ " " + "?????????ì????", new Font("????", Font.BOLD,
12),
Color.red, false, namelist,chatinfo.getIndex()); sendMessage(msginfo0, chatinfo);
while (true)
{
ois = new ObjectInputStream(s.getInputStream()); chatinfo = (ChatInfo) ois.readObject();
if (chatinfo.isExit())
{
list.remove(s);
namelist.remove(chatinfo.getNickname());
MessageInfo msginfo1 = new MessageInfo("", chatinfo .getNickname()
+ " " + "?????????ì????", new Font("????", Font.BOLD, 12),
Color.red, false, namelist,chatinfo.getIndex());
sendMessage(msginfo1, chatinfo);
oos = new ObjectOutputStream(s.getOutputStream()); MessageInfo exitInfo = new MessageInfo(true); // ???????§??
oos.writeObject(exitInfo);
s.close();
ois.close();
oos.close();
break;
} else
{
int index = namelist.indexOf(socketName);
namelist.remove(index);
namelist.insertElementAt(chatinfo.getNickname(), index);
socketName = chatinfo.getNickname();
MessageInfo msginfo = new MessageInfo(chatinfo
.getNickname(), chatinfo.getMessage(), chatinfo
.getFont(), chatinfo.getColor(), chatinfo .isWhisper(),
namelist,chatinfo.getIndex());
sendMessage(msginfo, chatinfo);
}
}
} catch (IOException e)
{
e.printStackTrace();
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public void sendMessage(MessageInfo minfo, ChatInfo cinfo) {
if (!cinfo.isWhisper())
{
try
{
for (Socket so : list)
{
oos = new ObjectOutputStream(so.getOutputStream()); // ???????§??
oos.writeObject(minfo);
}
} catch (IOException e)
{
e.printStackTrace();
}
} else
{
try
{
int count = 0;
for (Socket so : list)
{
if (count == cinfo.getIndex()
|| cinfo.getNetAddress().equals(
so.getInetAddress().toString())) {
ObjectOutputStream oos = new ObjectOutputStream(so
.getOutputStream());
// ???????§??
oos.writeObject(minfo);
}
count++;
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
class MessageInfo implements Serializable
{
private String msg, nickname;
private Font font;
private Color color;
private boolean whisper,exit;
private Vector<String> namelist;
private int index;
public MessageInfo(String nickname, String msg, Font font, Color color,
boolean whisper, Vector<String> namelist,int index) {
this.nickname = nickname; this.msg = msg; this.font = font; this.color = color; this.whisper = whisper; this.namelist = namelist; this.index = index; } public MessageInfo(boolean exit) { this.exit = exit; } public MessageInfo(String msg,boolean whisper) { this.msg = msg; this.whisper = whisper; } public String getNickname() { return nickname; } public String getMsg() { return msg; } public Font getFont() { return font; } public Color getColor() { return color; } public boolean isWhisper() { return whisper; } public Vector<String> getNamelist() {
} return namelist; } public int getIndex() { return index; } public boolean isExit() { return exit; }