📄 chatclientpanel.java
字号:
package net.yangzhe.chatSocket;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class ChatClientPanel extends JPanel{
private JTextPane jTextArea = null;
private JButton jButton = null;
private JTextPane jTextArea1 = null;
private JScrollPane jScrollPane = null;
private JScrollPane jScrollPane1 = null;
private JScrollPane jScrollPane3 = null;
private Client client;
private String show="";
private JButton jButton1,jButton3 = null;
private String nickname;
private JList jList=null;
private JLabel jLabel3=null;
private static int index=0;
SimpleAttributeSet attrset= new SimpleAttributeSet();
Vector <String> userList= new Vector<String>();
Document docs;
/**
* This is the default constructor
* @param client2
* @param c
* @throws IOException
* @throws UnknownHostException
*/
public ChatClientPanel(Client clienta,String un) throws UnknownHostException, IOException {
super();
//JFrame f=new JFrame(nickname+" :聊天室");
nickname=un;
initialize();
client =clienta;
//client.sendNews("%0%0");//jump out of cycle 1 at server
//client.sendNews(this.nickname);
Thread t=new Thread(new Runnable(){
public void run() {
while(true){
//System.out.println("abcd");
try {
show=client.getNews().replace("%8%8","\n");
outText(show);
} catch (Exception ex) {
// TODO Auto-generated catch block
//jTextArea1.setCaretPosition(index);
//jTextArea1.("失去对服务器的连接。");
outText("%2%2,"+now()+","+"以失去连接..\n");
ex.printStackTrace();
//this.stop();
break;
}
}
}
});
t.start();
Thread t2=new Thread(new Runnable(){
public void run() {
while(true){
//client.sendNews("%6%6");//refresh
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t2.start();
}
public SimpleAttributeSet color(int i){
SimpleAttributeSet attrset3=new SimpleAttributeSet();
switch(i){
case 1:StyleConstants.setForeground(attrset3,Color.blue);break; //时间蓝色
case 2:StyleConstants.setForeground(attrset3,Color.yellow);break; //成员黄色
case -1:StyleConstants.setForeground(attrset3,Color.red);break; //系统消息红色
default:
}
return attrset3;
}
public String now()
{
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("[ hh:mm:ss ]");
String dateString=null;
try
{
dateString = formatter.format(today);
return dateString;
} catch (IllegalArgumentException iae) {
//result.setForeground(Color.red);
// result.setText("Error: " + iae.getMessage());
}
return dateString;
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(null);
this.setSize(526, 470);
//this.setName(nickname+":聊天室");
jLabel3= new JLabel();
jLabel3.setBounds(new java.awt.Rectangle(430,17,56,47));
jLabel3.setText("在线列表");
this.add(jLabel3,null);
this.add(getJButton(), null);
this.add(getJScrollPane(), null);
this.add(getJScrollPane1(), null);
this.add(getJScrollPane3(),null);
this.add(getJButton1(), null);
this.add(getJButton3(),null);
//this.show();
//this.addWindowListener(new WindowAdapter(){
// public void windowClosing(WindowEvent e){
// System.exit(0);
// }
//});
}
private JScrollPane getJScrollPane3() {
if (jScrollPane3 == null) {
jScrollPane3 = new JScrollPane();
jScrollPane3.setBounds(new java.awt.Rectangle(425,60,75,150));
jScrollPane3.setViewportView(getJList());
}
return jScrollPane3;
}
private JList getJList() {
if (jList == null) {
jList = new JList();
jList.addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
//Vector <String> test=ChatServer.getUserList();
//jList.updateUI();
//
// if((index=jList.getSelectedValue())!=null)
// index=test[index1];
// jButton3.setEnabled(true);
// else jButton3.setEnabled(false);
}
});
}
return jList;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextPane getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextPane();
jTextArea.setText("");
jTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent e) {
if(e.isControlDown()&&e.getKeyCode()==10)
jButton.doClick();
//System.out.println("keyTyped()"+(KeyEvent.VK_ENTER+KeyEvent.VK_CONTROL)+" "+e.getKeyLocation()); // TODO Auto-generated Event stub keyTyped()
}
});
}
return jTextArea;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new java.awt.Rectangle(393,369,99,31));
jButton.setText("发送消息");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String s = "%3%3,"+now()+", "+nickname+" ,"+"说:"+jTextArea.getText().replace("\r\n","%8%8")+"%8%8";
if(s!=""||s!="\r\n")
client.sendNews(s);
jTextArea.setText("");
System.out.println(s+"kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
s=null;// TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton;
}
/**
* This method initializes jTextArea1
*
* @return javax.swing.JTextArea
*/
private JTextPane getJTextArea1() {
if (jTextArea1 == null) {
jTextArea1 = new JTextPane();
jTextArea1.setText("");
jTextArea1.setEditable(false);
}
return jTextArea1;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new java.awt.Rectangle(25,17,375,236));
jScrollPane.setViewportView(getJTextArea1());
}
return jScrollPane;
}
/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane1() {
if (jScrollPane1 == null) {
jScrollPane1 = new JScrollPane();
jScrollPane1.setBounds(new java.awt.Rectangle(25,266,475,95));
jScrollPane1.setViewportView(getJTextArea());
}
return jScrollPane1;
}
private JButton getJButton3(){
if(jButton3==null){
jButton3=new JButton();
jButton3.setBounds(new java.awt.Rectangle(170,368,108,32));
jButton3.setText("创建私聊");
jButton3.setEnabled(false);
}
return jButton3;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new java.awt.Rectangle(42,368,108,32));
jButton1.setText("我要离开了");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
client.sendNews("%end");//跳出服务器第2循环
/*try {
if(client.getNews()!=null)
System.exit(1);
} catch (IOException e1) {
// TODO Auto-generated catch block
System.exit(0);
//e1.printStackTrace();
}*/
System.exit(1);
System.out.println("aactionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
}
});
}
return jButton1;
}
void outText(String a){ //将String a输出
String[] info=a.split(",");
if(info[0].equals("%1%1")){ //用户列表
userList.clear();
for(int i=1;i<info.length;i++){
userList.add(info[i]);
System.out.println(info[i]);
}
jList.setListData(userList);
}else if(info[0].equals("%2%2")){ //系统消息
docs=jTextArea1.getDocument();
try{
docs.insertString(docs.getLength(),info[1],color(1)); //时间蓝色
jTextArea1.setCaretPosition(docs.getLength());
}catch(BadLocationException e){
e.printStackTrace();
}
String lm="";
for(int i=2;i<info.length;i++){
lm+=info[i];
if(i<info.length-1){
lm+=",";
}
}
//info=lm.split("%8%8");
//for(int i=0;i<info.length;i++){
docs=jTextArea1.getDocument();
try{
docs.insertString(docs.getLength(),lm,color(-1)); //系统红色
jTextArea1.setCaretPosition(docs.getLength());
}catch(BadLocationException e){
e.printStackTrace();
}
//}
}else if(info[0].equals("%3%3")){ //普通消息
docs=jTextArea1.getDocument();
try{
docs.insertString(docs.getLength(),info[1],color(1)); //时间蓝色
jTextArea1.setCaretPosition(docs.getLength());
}catch(BadLocationException e){
e.printStackTrace();
}
docs=jTextArea1.getDocument();
try{
docs.insertString(docs.getLength(),info[2],color(-1)); //名字红色
jTextArea1.setCaretPosition(docs.getLength());
}catch(BadLocationException e){
e.printStackTrace();
}
String lm="";
for(int i=3;i<info.length;i++){
lm+=info[i];
if(i<info.length-1){
lm+=",";
}
}
//info=lm.split("%8%8");
//for(int i=0;i<info.length;i++){
docs=jTextArea1.getDocument();
try{
docs.insertString(docs.getLength(),lm,color(10)); //普通
jTextArea1.setCaretPosition(docs.getLength());
}catch(BadLocationException e){
e.printStackTrace();
}
//}
}
}
//public static void main(String[] args) throws Exception{
// Client cl = new Client(InetAddress.getByName("127.0.0.1"));
// ChatClientPanel cc =new ChatClientPanel("127.0.0.1","test");
// cc.show();
//cc.show();
//cc.setVisible(true);
//}
} // @jve:decl-index=0:visual-constraint="10,10"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -