⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myclient.java

📁 用不同方法实现聊天室功能的JAVA代码
💻 JAVA
字号:
/*
 * MyClient.java
 *
 * Created on 2007年10月27日, 下午1:52
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package chatroom;
import java.awt.*; 
import java.awt.event.*; 
import java.net.*; 
import java.io.*; 

public class MyClient 
extends Frame 
implements ActionListener { 
Socket ClientSocket; 
PrintStream os; 
DataInputStream is; 
String s; 
Label Mylabel = new Label("欢迎使用本机提供的信息"); 
TextArea textArea; 
Button MyButton = new Button("发送"); 
public MyClient() { 
setTitle("Client Window"); 
setLayout(new BorderLayout()); 
this.addWindowListener(new WinAdptClient(this)); 
MyButton.addActionListener(this); 
textArea = new TextArea(20, 50); 
add("North", Mylabel); 
add("South", MyButton); 
add("Center", textArea); 
setResizable(false); 
pack(); 
show(); 
connect(); 
} 

public void connect() { 
try { 
ClientSocket = new Socket("xinjing", 8000); 
os = new PrintStream(new BufferedOutputStream(ClientSocket.getOutputStream())); 
is = new DataInputStream(new BufferedInputStream(ClientSocket.getInputStream())); 
s = is.readLine(); 
textArea.appendText(s + "\n"); 
} 
catch (Exception e) {} 
} 

public void actionPerformed(ActionEvent e) { 
if (e.getSource() == MyButton) { 
try { 
os.print(textArea.getText()); 
os.flush(); 
} 
catch (Exception ex) {} 
} 
} 

public static void main(String args[]) { 
new MyClient(); 
} 
} 

class WinAdptClient 
extends WindowAdapter { 
MyClient m_Parent; 

WinAdptClient(MyClient p) { 
m_Parent = p; 
} 

public void windowClosing(WindowEvent e) { 
try { 
m_Parent.os.println("Bye"); 
m_Parent.os.flush(); 
m_Parent.is.close(); 
m_Parent.os.close(); 
m_Parent.ClientSocket.close(); 
m_Parent.dispose(); 
System.exit(0); 
} 
catch (Exception ex) {} 
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -