📄 udpsend.java
字号:
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class UDPSend extends Frame implements ActionListener
{
TextField TextField1=new TextField();
Button button1=new Button();
Button button2=new Button();
public UDPSend()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e)
{
Component com=(Component)e.getSource();
if(com.equals(button1))
sendData();
else if(com.equals(button2))
System.exit(0);
else
sendData();
}
void sendData()
{
try
{
String msg=TextField1.getText();
if(msg.equals(""))
return;
TextField1.setText("");
InetAddress address=InetAddress.getByName("192.168.18.2");
int len=msg.length();
byte[] message=new byte[len];
msg.getBytes(0,len,message,0);
DatagramPacket packet=new DatagramPacket(message,len,address,9999);
DatagramSocket socket=new DatagramSocket();
socket.send(packet);
}
catch(Exception e)
{
}
}
public static void main(String[] args)
{
UDPSend send=new UDPSend();
send.setLocation(100,100);
send.setSize(200,120);
send.show();
}
private void jbInit() throws Exception
{
this.setTitle("发送数据报");
button1.setLabel("发送");
button2.setLabel("退出");
//this.setLayout(new FlowLayout());
this.add(TextField1,BorderLayout.CENTER);
this.add(button1,BorderLayout.NORTH);
this.add(button2,BorderLayout.SOUTH);
button1.addActionListener(this);
button2.addActionListener(this);
TextField1.addActionListener(this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -