```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.*;
public class TestFrame01 extends JFrame {
JTextArea jta=new JTextArea();
JTextField jtf=new JTextField(15);
JPanel jp=new JPanel();
JButton jb=new JButton("发送");
DatagramSocket ds;
DatagramPacket dp;
String id;
TestFrame01 tf=this;
int port;
public TestFrame01( String id,int port) throws IOException {
this.port=port;
this.id=id;
init();
operation();
Receive();
}
public void init() throws UnknownHostException {
this.setTitle("窗口1");
this.setSize(500,500);
jp.add(jtf);
jp.add(jb);
// 创建一个JScrollPane,并将JTextArea添加到其中
JScrollPane scrollPane = new JScrollPane(jta);
this.add(scrollPane, "Center");
this.add(jp,"South");
this.setDefaultCloseOperation(3);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void operation() throws IOException {
//发送给自己和另一个窗口
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb) {
byte[] bytes = jtf.getText().trim().getBytes();
try {
ds = new DatagramSocket();
dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(id), port);
ds.send(dp);
ds.close();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
jta.setEditable(false);
jta.setFont(new Font("宋体", Font.BOLD, 25));
jta.setForeground(Color.red);
jta.append("我:" + jtf.getText().trim());
jta.append("\n");
}
}
});
}
public void Receive(){
try {
jta.setEditable(false);
jta.setFont(new Font("宋体", Font.BOLD, 25));
jta.setEditable(false);
while (true) {
byte[] bytes = new byte[1024];
ds = new DatagramSocket(10087);
dp = new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
String message = new String(dp.getData(), 0, dp.getLength());
jta.append("窗口2发送了信息:" + message);
jta.append("\n");
ds.close();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public static void main(String[] args) throws IOException {
new TestFrame01("127.0.0.1",10086);
}
}
public class TestFrame02 extends JFrame{
JTextArea jta=new JTextArea();
JTextField jtf=new JTextField(15);
JPanel jp=new JPanel();
JButton jb=new JButton("发送");
DatagramSocket ds;
DatagramPacket dp;
String sendID;
int sendPORT;
public TestFrame02( String sendID,int port) throws IOException {
this.sendID=sendID;
this.sendPORT=port;
init();
operation();
Receive();
}
public void init() throws UnknownHostException {
this.setTitle("窗口2");
this.setSize(500,500);
jp.add(jtf);
jp.add(jb);
// 创建一个JScrollPane,并将JTextArea添加到其中
JScrollPane scrollPane = new JScrollPane(jta);
this.add(scrollPane, "Center");
this.add(jp,"South");
this.setDefaultCloseOperation(3);
this.setAlwaysOnTop(true);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void operation() throws IOException {
//发送给自己和另一个窗口
jb.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb){
byte[] bytes = jtf.getText().trim().getBytes();
try {
ds=new DatagramSocket();
dp=new DatagramPacket(bytes,bytes.length,InetAddress.getByName(sendID),sendPORT);
ds.send(dp);
ds.close();
} catch (Exception ex) {
throw new RuntimeException(ex);
}
jta.setFont(new Font("宋体",Font.BOLD,25));
jta.setForeground(Color.red);
jta.append("我:"+jtf.getText().trim());
jta.append("\n");
}
}
});
}
public void Receive(){
jta.setFont(new Font("宋体",Font.BOLD,25));
jta.setForeground(Color.red);
jta.setEditable(false);
while (true) {
try {
ds=new DatagramSocket(10086);
byte[]bytes=new byte[1024];
dp=new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
String message=new String(dp.getData(),0,dp.getLength()) ;
jta.append("窗口1发送了信息:"+message);
jta.append("\n");
ds.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
public static void main(String[] args) throws IOException {
new TestFrame02("127.0.0.1",10087);
}
}
窗口1:ip本机 发送端口10086 接收端口10087
窗口2:ip本机 发送端口10087 接收端口10086
```