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

📄 internet.java

📁 用Java编写的一个简单的通讯程序
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Internet implements WindowListener,ActionListener
{
public static void main(String args[])
{
Test a=new Test();
a.MyWindow();
}
private Frame win;
private TextField IPtext;
private TextArea textarea1,textarea2;
private Button connection,send;
private Socket clientsocket;
private ServerSocket serversocket;
private Client client;
private Server server;
private DataInputStream in=null;
private DataOutputStream out=null;
private String name=null;
public void MyWindow()
{
server=new Server();
server.start();
win=new Frame("JAVA局域网聊天程序");
win.setSize(350,500);
win.setResizable(false);
win.setLocation(330,150);
win.setVisible(true);
win.setLayout(new FlowLayout());
IPtext=new TextField(16);
connection=new Button("连接");
win.add(new Label("连接IP:"));
win.add(IPtext);
connection.addActionListener(this);
win.add(connection);
Panel panel2=new Panel();
panel2.setLayout(new BorderLayout());
panel2.add(new Label("聊天记录:"),"North");
textarea1=new TextArea(15,40);
textarea1.setEditable(false);
panel2.add(textarea1,"Center");
win.add(panel2);
Panel panel3=new Panel();
panel3.setLayout(new BorderLayout());
textarea2=new TextArea(5,40);
textarea2.addKeyListener(new KeyEventHandler());
panel3.add(new Label("聊天输入:"),"North");
panel3.add(textarea2,"Center");
win.add(panel3);
send=new Button("发送(Ctrl+Enter)");
win.add(send);
send.addActionListener(this);
win.validate();
new PopWindow();
win.addWindowListener(this);
}
public void actionPerformed(ActionEvent e)
{
String sendStr=null;
if(e.getSource()==connection)
{
try{
serversocket.close();
server.interrupt();
}
catch(Exception e1){};
out=null;
client=new Client(IPtext.getText());
client.start();
}
else if(e.getSource()==send)
{
if(out!=null)
{
sendStr=textarea2.getText();
textarea2.setText("");
sendStr=name+sendStr+"\r\n\r\n";
textarea1.append(sendStr);
try{out.writeUTF(sendStr);}
catch(IOException e1){IPtext.setText("连接错误");}
}
else IPtext.setText("还未建立连接");
}
}
class Server extends Thread
{
private String recvStr=null;
public void run()
{
try
{
serversocket=new ServerSocket(6858);
}
catch(IOException e1){IPtext.setText("服务器建立失败~");}
try
{
clientsocket=serversocket.accept();
in=new DataInputStream(clientsocket.getInputStream());
out=new DataOutputStream(clientsocket.getOutputStream());
IPtext.setText("连接建立,可以通讯");
while(true)
{
recvStr=in.readUTF();
textarea1.append(recvStr);
Thread.sleep(500);
}
}
catch(IOException e){}
catch(InterruptedException e){}
}
}
class Client extends Thread
{
private String serverIP=null;
private String recvStr=null;
Client(String serverIP)
{
this.serverIP=serverIP;
}
public void run()
{
try
{
 IPtext.setText("连接建立中...");
 clientsocket=new Socket(serverIP,6858);
 IPtext.setText("连接建立,可以通讯");
 in=new DataInputStream(clientsocket.getInputStream());
 out=new DataOutputStream(clientsocket.getOutputStream());
 while(true)
 {
 recvStr=in.readUTF();
 textarea1.append(recvStr);
 Thread.sleep(500);
 }
}
catch(IOException e){IPtext.setText("连接建立失败");}
catch(InterruptedException e){IPtext.setText("连接中断");}
}
}
class KeyEventHandler implements KeyListener
{
private boolean pressed=false;
String sendStr=null;
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_CONTROL)
pressed=true;
}
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER&&pressed)
{
pressed=false;
if(out!=null)
{
sendStr=textarea2.getText();
textarea2.setText("");
sendStr=name+sendStr+"\r\n\r\n";
textarea1.append(sendStr);
try{out.writeUTF(sendStr);}
catch(IOException e1){IPtext.setText("连接错误");}
}
else IPtext.setText("还未建立连接");
}
}
public void keyTyped(KeyEvent e){}
}
class PopWindow implements ActionListener
{
	private Frame popwindow;
	private TextField nametext;
	private Button btn;
PopWindow()
	{
	popwindow=new Frame("");
	popwindow.setSize(150,120);
	popwindow.setLayout(new FlowLayout());
        popwindow.setLocation(430,300);
	popwindow.setVisible(true);
        popwindow.setResizable(false);
	popwindow.add(new Label("请输入您的昵称:"));
        nametext=new TextField(15);
	popwindow.add(nametext);
	btn=new Button("确定");
	popwindow.add(btn);
        btn.addActionListener(this);
	popwindow.validate();
	}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==btn)
{
name=nametext.getText();
if(name.equals(""))name="未知昵称";
name=name+":\r\n";
popwindow.dispose();
}
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}

⌨️ 快捷键说明

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