📄 chatapplet.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.anchor = GridBagConstraints.NORTHWEST;
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)
{}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -