📄 chatapplet.java.bak
字号:
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;
static int membernumber = 0;
}
public class ChatApplet extends JApplet implements ActionListener, MouseListener
{
newpanel loginPanel;//登陆面板,网格袋布局
JPanel mainPanel; //主面板,卡片布局
JPanel chatPanel; //聊天面板,边界布局
JLabel labelLoginName; //登陆面板中的组件
JLabel labelPassword;
JTextField textLoginName;
JPasswordField textPassword;
JButton buttonLogin;
JButton buttonRewrite;
GridBagLayout gbObject;
GridBagConstraints gbc;
JPanel panel1; //聊天面板中的4个子面板
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 newpanel();
//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); //将2个子面板加到主面板中
mainPanel.add("two", chatPanel);
((CardLayout)mainPanel.getLayout()).show(mainPanel, "one"); //显示第一页登陆面板
gbObject = new GridBagLayout(); //设置登陆面板为网格袋布局
gbc = new GridBagConstraints();
loginPanel.setLayout(gbObject);
labelLoginName = new JLabel("姓名:");
labelPassword = new JLabel("密码:");
textLoginName = new JTextField(15);
textPassword = new JPasswordField(15);
buttonLogin = new JButton("登陆");
buttonLogin.setBackground(new Color(100, 160, 200));
buttonLogin.addActionListener(new ActionListener() //对按钮添加动作监听并重载方法
{
int type = JOptionPane.PLAIN_MESSAGE;
String message = "";
String title = "错误";
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonLogin) //当点击按钮时
{
if (textLoginName.getText().equals("")) //如果姓名为空
{
type = JOptionPane.ERROR_MESSAGE;
message = "登陆名字不能为空!";
JOptionPane.showMessageDialog(loginPanel, message, title, type); //用弹出对话框提示
}
else if (new String(textPassword.getPassword()).equals("")) //如果密码为空
{
type = JOptionPane.ERROR_MESSAGE;
message = "密码不能为空!";
JOptionPane.showMessageDialog(loginPanel, message, title, type);
}
else
{
name = textLoginName.getText(); //获得名字
((CardLayout)mainPanel.getLayout()).show(mainPanel, "two"); //显示第二页,进入聊天面板
try
{
clientSocket = new Socket("127.0.0.1", 7000);
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("重写");
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);
Localtime localtime = new Localtime();
localtime.start();//启动线程添加本机时间
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(); //来存储JList的显示数据
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); //将列表添加到子面板4中
listName.addMouseListener(this); //对列表栏进行鼠标监听
textName =new JTextField(8);
textName.setEditable(false); //使文本栏不可编辑
textName.setText("所有人"); //添加内容
textObject = new JTextField(40);
textObject.addKeyListener(new KeyAdapter() //对文本栏进行键盘监听
{
public void keyPressed(KeyEvent e) //重载其中的一种方法
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
buttonSend.doClick();
}
}
});
labelTitle = new JLabel(" ");
TitleText title = new TitleText();
title.start();
//labelTitle.setFont(new Font("华文行楷", Font.PLAIN, 30));
labelTo = new JLabel("对");
labelSaid = new JLabel("说:");
buttonSend = new JButton("发送");
buttonSend.setBackground(new Color(160, 220, 160));
buttonSend.addActionListener(this); //对按钮进行动作监听
buttonExit = new JButton("退出聊天室");
buttonExit.setBackground(new Color(160, 220, 160));
buttonExit.addActionListener(this); //对按钮进行动作监听
chatPanel.setLayout(new BorderLayout(5, 10)); //设置聊天面板为边界布局
chatPanel.add(panel1, BorderLayout.SOUTH); //将子面板1添加到聊天面板的南面
panel1.setLayout(new FlowLayout(FlowLayout.LEFT)); //设置子面板1为流布局,并左对齐
panel1.add(labelTo); //添加组件
panel1.add(textName);
panel1.add(labelSaid);
panel1.add(textObject);
panel1.add(buttonSend);
panel1.add(buttonExit);
chatPanel.add(panel3, BorderLayout.NORTH); //将子面板3添加到聊天面板的北面
panel3.setLayout(new FlowLayout()); //设置子面板3为流布局
panel3.add(labelTitle);
chatPanel.add(panel2, BorderLayout.CENTER); //将子面板2添加到聊天面板的中间
panel2.setLayout(new BorderLayout(10, 15)); //设置子面板2设置为边界布局
panel2.add("East", textChat); //将聊天栏放在东面
panel2.add("Center", panel4); //将子面板4放在中间
}
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();
if (textObject.getText().equals(""))
{
textMain.append("Sorry,不能发送空信息!!\n");
textMain.setCaretPosition(textMain.getDocument().getLength());
return;
}
if (textName.getText().equals(name))
{
textMain.append("Sorry,公共场合请勿自言自语!!\n");
textMain.setCaretPosition(textMain.getDocument().getLength());
textObject.setText("");
return;
}
out.writeObject((Customer)cust1); //写出对象
textObject.setText("");
}
catch(Exception e1)
{}
}
if (e.getSource() == buttonExit)
{
int type = JOptionPane.PLAIN_MESSAGE;
String message = "";
String title = "退出";
try
{
cust1.custSign = 3;
cust1.custName = name;
out.writeObject((Customer)cust1);
clientSocket.close();
type = JOptionPane.INFORMATION_MESSAGE;
message = "您已退出聊天室,欢迎再次光临";
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)
{
Date d=new Date();
GregorianCalendar c=new GregorianCalendar();
c.setTime(d);
name = temp.custName;
message = temp.sendMessage;
textMain.append(c.get(Calendar.HOUR_OF_DAY)+":"+c.get(Calendar.MINUTE)+":"+c.get(Calendar.SECOND)+" <" + name + ">" + "对" + "<" + temp.otherName + "> " + "说:" + message + "\n");
textMain.setCaretPosition(textMain.getDocument().getLength());//出现在文本域的最下方
}
if (temp.custSign == 2 || temp.custSign == 3)
{
listModel.clear(); //清空列表
listModel.addElement("所有人"); //加入一个元素
String onlineName;
for (int i = 0; i < temp.nameList.size(); i++ )//将所有的元素加入
{
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 paint(Graphics g)
{
g.drawString(temp.membernumber+"zai",20,20);
}
class Localtime extends Thread
{
Date date;
GregorianCalendar calendar;
String strdate,strtime,strstatus;
public void run()
{
while (true)
{
display();
try
{
this.sleep(1000);
}
catch(InterruptedException e)
{
getAppletContext().showStatus("Thread interrupted");
}
}
}
public void display()
{
date=new Date();
calendar=new GregorianCalendar();
calendar.setTime(date);
strtime=calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
strdate=(calendar.get(Calendar.MONTH)+1)+"/"+calendar.get(Calendar.DATE)+"/"+calendar.get(Calendar.YEAR);
strstatus=strtime+" "+strdate;
showStatus(strstatus);
}
}
class TitleText extends Thread
{
String Info[]={"欢迎光临聊天室","让我们畅所欲言吧"};
int counter=0;
public void run()
{
while (true)
{
display();
try
{
this.sleep(1500);
}
catch(InterruptedException e)
{
getAppletContext().showStatus("Thread interrupted");
}
}
}
public void display()
{
labelTitle.setFont(new Font("华文行楷", Font.PLAIN, 30));
labelTitle.setText(Info[counter]);
counter++;
if(counter>=2)
counter=0;
}
}
public void destroy()
{
try
{
Customer cust1 = new Customer();
cust1.custSign = 3;
cust1.custName = name;
out.writeObject((Customer)cust1);
clientSocket.close();
}
catch(Exception e)
{}
}
}
class newpanel extends JPanel
{
public newpanel(){}
public void paintComponent(Graphics g)
{
int x=0,y=0;
ImageIcon icon=new ImageIcon("01.jpg");
g.drawImage(icon.getImage(),x,y,getSize().width,getSize().height,this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -