📄 windows.java
字号:
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.text.DecimalFormat;
interface Module
{
TextArea display=new TextArea(12,65);
TextArea write=new TextArea(8,65);
Button send=new Button("发送");
Button close=new Button("关闭");
Button talk=new Button("语音聊天");
boolean start=true;
}
public class Windows extends Frame implements ActionListener,Module
{
Windows()
{
super("聊天工具");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(500,500);
setLocation(300,300);
setFont(new Font("Arial",Font.PLAIN,12));
setLayout(new FlowLayout());
display.setEditable(false);
add(display);
add(write);
add(send);
add(close);
add(talk);
send.addActionListener(this);
close.addActionListener(this);
talk.addActionListener(this);
show();
}
public static void main(String args[])throws IOException
{
new Windows();
new RecieveThread().start();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==send)
{
String s;
if((write.getText().toString()).equals(""))
{
JOptionPane.showMessageDialog(null,"发送的内同不能为空!","注意",JOptionPane.INFORMATION_MESSAGE);
}
else
{
display.append("老大:"+(new Date().toString())+"\n"+write.getText()+"\n");
write.setText("");
new SendThread().start();
}
}
if(e.getSource()==close)
{
System.exit(0);
}
}
}
class SendThread extends Thread implements Module
{
public SendThread()
{
super("发送线程");
}
byte [] buf=new byte[256];
DatagramSocket socket=null;
DatagramPacket packet=null;
public void run()
{
try
{
String s=write.getText();
byte [] buf=new byte[256];
InetAddress address=InetAddress.getByName("122.96.88.93");
DatagramPacket packet=new DatagramPacket(buf,buf.length,address,4445);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
}
catch(IOException e)
{
display.append("无法建立连接!\n");
}
}
}
class RecieveThread extends Thread implements Module
{
public RecieveThread()
{
super("接收线程");
}
byte [] buf=new byte[256];
DatagramSocket socket=null;
DatagramPacket packet=null;
public void run()
{
try
{
socket=new DatagramSocket();
packet=new DatagramPacket(buf,buf.length);
}
catch(IOException e)
{
}
while(true)
{
try
{
socket.receive(packet);
String received=new String(packet.getData());
String s="楼新月: "+(new Date().toString())+"\n"+received+"\n";
display.append(s);
}
catch(IOException e)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -