📄 client.java~13~
字号:
package chat;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.net.*;
import java.io.*;
/**
*
* <p>Title:客户端applet类 </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class Client extends JApplet implements Runnable{
Socket socket=null;
DataInputStream dis=null;
DataOutputStream dos=null;
Hashtable peopleList;
Thread clientThread=null;
private boolean isStandalone = false;
ChatArea chatArea;
//Get a parameter value
/*public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}*/
//Construct the applet
public Client() {
}
//Initialize the applet
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
String userHome=System.getProperty("user.home");
File fileDir = new File(userHome, "聊天记录");
if (!fileDir.exists()) {
fileDir.mkdir();
}
peopleList=new Hashtable();
chatArea=new ChatArea(peopleList);
this.setSize(new Dimension(400,300));
this.getContentPane().add(chatArea, BorderLayout.CENTER);
}
public void start(){
if(socket!=null&&dos!=null&dis!=null){
try{
socket.close();
dos.close();
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}
try{
socket=new Socket(this.getCodeBase().getHost(),4331);
dos=new DataOutputStream(socket.getOutputStream());
dis=new DataInputStream(socket.getInputStream());
}catch(IOException e){
chatArea.loginDialog.setVisible(false);
JOptionPane.showMessageDialog(null, "网络连接错误,请重新登陆", "提示信息",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
// e.printStackTrace();
}
if(socket!=null){
chatArea.loginDialog.setConnection(socket,dis,dos);
//System.out.println(socket.getPort()+"client");
}
if(clientThread==null){
clientThread=new Thread(this);
clientThread.start();
}
}
public void run(){
while(clientThread!=null){
if(chatArea.loginDialog.getEnableChat()){
chatArea.setConnection(socket,dos,dis);
break;
}
try{
Thread.sleep(1000);
}catch(Exception e){
//e.printStackTrace();
}
}
}
public void stop(){
try{
this.socket.close();
this.dos.flush();
this.dos.close();
this.dis.close();
this.clientThread=null;
}catch(Exception e){
// e.printStackTrace();
}
}
//Get Applet information
/* public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -