解决方案 »
- 提一个面向对象开发的百思不得其解的问题
- [求助]同一个包,用javac *.java可以编译的文件,为什么单个编译时出错?
- JLabel边框如何显示出来?
- 编译子类时,父类应该放在哪里?
- 有关James配置数据库的问题,请高手进入!
- 200分求 Together for eclipse 6.2 的注册机
- 一个关于成员变量初始和方法变量初始的问题!
- Jbuilder5中碰到的问题?
- 关于画图的问题!!!
- 大家好:org.springframework.beans.factory.NoUniqueBeanDefinitionException
- 如何写出二进制文件问题。请精通c++和java的高手进来!
- 哪里有比较有名的电脑培训?
//ChatApplet.java
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.net.*;
import java.io.*;class Customer implements Serializable
{
String infoMessage = "";
String sendMessage = "";
String custName = "";
String otherName= "";
Vector nameList = new Vector(1, 1);
int custSign = 0;
}public class ChatApplet extends JApplet implements ActionListener, MouseListener
{
JPanel loginPanel;
JPanel mainPanel;
JPanel chatPanel;JLabel labelLoginName;
JLabel labelPassword;JTextField textLoginName;
JPasswordField textPassword;JButton buttonLogin;
JButton buttonRewrite;GridBagLayout gbObject;
GridBagConstraints gbc;JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;JLabel labelTitle;
JLabel labelTo;
JLabel labelSaid;
JButton buttonSend;
JButton buttonExit;JList listName;
DefaultListModel listModel;
JTextArea textMain;
JTextField textName;
JTextField textObject;String name;
Socket clientSocket;
ObjectInputStream in;
ObjectOutputStream out;public void init()
{
mainPanel = new JPanel();
loginPanel = new JPanel();
loginPanel.setBackground(new Color(80, 170, 200));
chatPanel = new JPanel();
chatPanel.setBackground(new Color(160, 220, 160));
this.getContentPane().add(mainPanel);mainPanel.setLayout(new CardLayout());
mainPanel.add("one", loginPanel);
mainPanel.add("two", chatPanel);
((CardLayout)mainPanel.getLayout()).show(mainPanel, "one");gbObject = new GridBagLayout();
gbc = new GridBagConstraints();
loginPanel.setLayout(gbObject);labelLoginName = new JLabel("Login Name ");
labelPassword = new JLabel("Password ");
textLoginName = new JTextField(15);
textPassword = new JPasswordField(15);buttonLogin = new JButton("Login");
buttonLogin.setBackground(new Color(100, 160, 200));
buttonLogin.addActionListener(new ActionListener()
{
int type = JOptionPane.PLAIN_MESSAGE;
String message = "";
String title = "Error";public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonLogin)
{
if (textLoginName.getText().equals(""))
{
type = JOptionPane.ERROR_MESSAGE;
message = "Cannot login name empty!";
JOptionPane.showMessageDialog(loginPanel, message, title, type);
}
else if (new String(textPassword.getPassword()).equals(""))
{
type = JOptionPane.ERROR_MESSAGE;
message = "Cannot password empty!";
JOptionPane.showMessageDialog(loginPanel, message, title, type);
}
else
{
name = textLoginName.getText();
((CardLayout)mainPanel.getLayout()).show(mainPanel, "two");try
{
clientSocket = new Socket("127.0.0.1", 1001);
out = new ObjectOutputStream(clientSocket.getOutputStream());
Customer cust = new Customer();
cust.custName = name;
cust.custSign = 2;
out.writeObject((Customer)cust);
}
catch (UnknownHostException ue)
{
System.err.println("Unknown Host Name");
System.exit(0);
}
catch (IOException evt)
{
System.err.println("Cannot write to the server");
System.exit(0);
}Message message = new Message();
message.start();
}
}
}
});buttonRewrite = new JButton("Rewrite");
buttonRewrite.setBackground(new Color(100, 160, 200));
buttonRewrite.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonRewrite)
{
textLoginName.setText("");
textPassword.setText("");
}
}
});
gbc.gridx = 2;
gbc.gridy = 5;
gbObject.setConstraints(labelLoginName, gbc);
loginPanel.add(labelLoginName);gbc.gridx = 4;
gbObject.setConstraints(textLoginName, gbc);
loginPanel.add(textLoginName);gbc.gridx = 2;
gbc.gridy = 8;
gbObject.setConstraints(labelPassword, gbc);
loginPanel.add(labelPassword);gbc.gridx = 4;
gbObject.setConstraints(textPassword, gbc);
loginPanel.add(textPassword);gbc.gridx = 2;
gbc.gridy = 11;
gbObject.setConstraints(buttonLogin, gbc);
loginPanel.add(buttonLogin);gbc.gridx = 4;
gbObject.setConstraints(buttonRewrite, gbc);
loginPanel.add(buttonRewrite);panel1 = new JPanel();
panel1.setBackground(new Color(160, 220, 160));
panel2 = new JPanel();
panel2.setBackground(new Color(160, 220, 160));
panel3 = new JPanel();
panel3.setBackground(new Color(150, 180, 220));
panel4 = new JPanel();
panel4.setBackground(new Color(160, 220, 160));textMain = new JTextArea(20, 87);
textMain.setEditable(false);
textMain.setLineWrap(true);
textMain.setFont(new Font("宋体", Font.PLAIN, 14));
JScrollPane textChat = new JScrollPane(textMain);listModel = new DefaultListModel();
listName = new JList(listModel);
listName.setFont(new Font("宋体", Font.PLAIN, 14));
listName.setVisibleRowCount(22);
listName.setFixedCellWidth(160);
listName.setFixedCellHeight(25);
JScrollPane list = new JScrollPane(listName);
panel4.add(list);
listName.addMouseListener(this);textName =new JTextField(8);
textName.setEditable(false);
textName.setText("everybody");textObject = new JTextField(40);
textObject.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
buttonSend.doClick();
}
}
});labelTitle = new JLabel(" CHAT ROOM ");
labelTitle.setFont(new Font("Arial Black", Font.PLAIN, 25));labelTo = new JLabel("To");
labelSaid = new JLabel("Said");buttonSend = new JButton("Send");
buttonSend.setBackground(new Color(160, 220, 160));
buttonSend.addActionListener(this);buttonExit = new JButton("Exit");
buttonExit.setBackground(new Color(160, 220, 160));
buttonExit.addActionListener(this);chatPanel.setLayout(new BorderLayout(5, 10));
chatPanel.add(panel1, BorderLayout.SOUTH);
panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
panel1.add(labelTo);
panel1.add(textName);
panel1.add(labelSaid);
panel1.add(textObject);
panel1.add(buttonSend);
panel1.add(buttonExit);chatPanel.add(panel3, BorderLayout.NORTH);
panel3.setLayout(new FlowLayout());
panel3.add(labelTitle);chatPanel.add(panel2, BorderLayout.CENTER);
panel2.setLayout(new BorderLayout(10, 15));
panel2.add("East", textChat);
panel2.add("Center", panel4);
}public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}public void mouseClicked(MouseEvent e)
{
String temp;
int index;if (e.getSource() == listName)
{
if (e.getClickCount() == 2)
{
index = listName.locationToIndex(e.getPoint());
temp = (String)listModel.getElementAt(index);
textName.setText(temp);
}
}
}public void actionPerformed(ActionEvent e)
{
Customer cust1 = new Customer();if (e.getSource() == buttonSend)
{
try
{
cust1.otherName = textName.getText();
cust1.custSign = 1;
cust1.custName = name;
cust1.sendMessage = textObject.getText();
out.writeObject((Customer)cust1);
textObject.setText("");
}
catch(Exception e1)
{}
}if (e.getSource() == buttonExit)
{
int type = JOptionPane.PLAIN_MESSAGE;
String message = "";
String title = "exit";try
{
cust1.custSign = 3;
cust1.custName = name;
out.writeObject((Customer)cust1);
clientSocket.close();
type = JOptionPane.INFORMATION_MESSAGE;
message = "You quit chat room!";
JOptionPane.showMessageDialog(loginPanel, message, title, type);
System.exit(0);
}
catch(Exception evt)
{}
}
}class Message extends Thread
{
public void run()
{
while (true)
{
try
{
String name;
String message;
in = new ObjectInputStream(clientSocket.getInputStream());
Customer temp = (Customer)in.readObject();if (temp.custSign == 1)
{
name = temp.custName;
message = temp.sendMessage;
textMain.append("<" + name + ">" + " said to " + "<" + temp.otherName + "> " + message + "\n");
textMain.setCaretPosition(textMain.getDocument().getLength());
}if (temp.custSign == 2 || temp.custSign == 3)
{
listModel.clear();
listModel.addElement("everybody");for (int i = 0; i < temp.nameList.size(); i++ )
{
String onlineName;
onlineName = (String) temp.nameList.elementAt(i);
listModel.addElement(onlineName);
System.out.println(onlineName);
}textMain.append(temp.infoMessage + "\n");
textMain.setCaretPosition(textMain.getDocument().getLength());
}if (temp.custSign == 4)
{
textMain.append(temp.sendMessage + "\n");
textMain.setCaretPosition(textMain.getDocument().getLength());
}
}
catch(Exception e)
{}
}
}
}public void destroy()
{
int type = JOptionPane.PLAIN_MESSAGE;
String message = "";
String title = "exit";try
{
Customer cust1 = new Customer();
cust1.custSign = 3;
cust1.custName = name;
out.writeObject((Customer)cust1);
type = JOptionPane.INFORMATION_MESSAGE;
message = "You quit chat room!";
JOptionPane.showMessageDialog(loginPanel, message, title, type);
clientSocket.close();
}
catch(Exception e)
{}
}
}
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.net.*;
import java.io.*;class Customer implements Serializable
{
String infoMessage;
String sendMessage;
String custName;
String otherName;
Vector nameList = new Vector(1, 1);
int custSign = 0;
}public class AppServer extends Thread
{
ServerSocket server;
static Vector sumName = new Vector(1, 1);
static Vector sumList = new Vector(1, 1);public AppServer()
{
try
{
server = new ServerSocket(1001);
System.out.println(server.toString());
}
catch (IOException e)
{
System.out.println("Cannot start the thread " + e);
}System.out.println("Server started.....");
this.start();
}public static void main(String args[])
{
new AppServer();
}public void run()
{
try
{
while (true)
{
Socket client = server.accept();
sumName.addElement(client);
Connection con = new Connection(client);
System.out.println("From:" + client.toString() + "Connected");
}
}
catch (Exception e)
{
System.out.println("Cannot listen to the client " + e);
}
}
}class Connection extends Thread
{
protected Socket netClient;
protected ObjectInputStream toServer;
protected ObjectOutputStream toClient;public Connection(Socket client)
{
netClient = client;try
{
toServer = new ObjectInputStream(netClient.getInputStream());
}
catch (IOException e)
{
System.out.println("IO Exception" + e);try
{
netClient.close();
}
catch (IOException evt)
{
System.out.println("Cannot closed" + evt);
}
}this.start();
}public void run()
{
Socket socket;
Customer temp;try
{
while (true)
{
temp = (Customer) toServer.readObject();if (temp.custSign == 2)
{
AppServer.sumList.addElement(temp.custName);
temp.nameList = AppServer.sumList;
temp.infoMessage = "> > > > > > > > > > " + "Welcome " + temp.custName + " to chat room!" + " < < < < < < < < < <";
}if (temp.custSign == 3)
{
int index = AppServer.sumList.indexOf(temp.custName);
AppServer.sumList.removeElementAt(index);
temp.nameList = AppServer.sumList;
System.out.println(temp.custName + " Exit!");
AppServer.sumName.removeElementAt(index);
temp.infoMessage = "> > > > > > > > > > " + temp.custName + " have left!" + " < < < < < < < < < <";
}if (!temp.otherName.equals("") && !temp.otherName.equals("everybody"))
{
if (AppServer.sumList.contains(temp.otherName))
{
for (int i = 0; i < AppServer.sumName.size(); i++)
{
socket = (Socket) AppServer.sumName.elementAt(i);
toClient = new ObjectOutputStream(socket.getOutputStream());
toClient.writeObject((Customer)temp);
}
}
else
{
int sum = AppServer.sumList.indexOf(temp.custName);
temp.sendMessage = "Sorry, " + temp.otherName + "have left!";
temp.custSign = 4;
socket = (Socket) AppServer.sumName.elementAt(sum);
toClient = new ObjectOutputStream(socket.getOutputStream());
toClient.writeObject((Customer)temp);
}
}
else
{
for (int i = 0; i < AppServer.sumName.size(); i++)
{
socket = (Socket) AppServer.sumName.elementAt(i);
toClient = new ObjectOutputStream(socket.getOutputStream());
toClient.writeObject((Customer)temp);
}
}
}
}
catch(Exception e)
{
System.out.println("Exception " + e);
}
}
}
随便输入一个用户名,就可以聊天了。