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

📄 chatx.java

📁 java Net,聊天及多线程实例源代码
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class ChatX
{
	static JFrame jf=new JFrame("聊天中X");
    static final JTextField jtf=new JTextField(30);
	static JButton jb=new JButton("发送");
	static JTextArea jta=new JTextArea(10,40);
	public static void main(String[] args) throws Exception
	{
		setFrame();
		chat();	
	}
	public static void setFrame()
	{
		
		jf.getContentPane().setLayout(new FlowLayout());
		jf.setSize(500,300);
		jf.setLocation(100,100);
		jf.getContentPane().add(jtf);
		jf.getContentPane().add(jb);
		jf.getContentPane().add(jta);
		jf.show();
		jf.addWindowListener(new WindowAdapter()

		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
	}
	public static void chat() throws Exception
	{
		ServerSocket ss=new ServerSocket(6000);
		Socket s=ss.accept();
		final InputStream is=s.getInputStream();
		final OutputStream os=s.getOutputStream();
			
		jb.addActionListener( new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{   
				    String strChat=jtf.getText();
				    try{
				    	os.write(strChat.getBytes()); 
				    }catch(IOException ex)
				    {
				    	ex.printStackTrace();
				    }
				    try{
				    	byte[] buf=new byte[100];
					    int len=is.read(buf);
						String strFromX=new String(buf,0,len);
						jta.setText(strFromX);
				    }catch(Exception ex)
				    {
				    	ex.printStackTrace();
				    }
					
				}
			});
		/*byte[] buf=new byte[100];
		int len=is.read(buf);
		String strFromY=new String(buf,0,len);
		jta.setText(strFromY);*/
		
	}
}

⌨️ 快捷键说明

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