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

📄 frame1.java~38~

📁 java灵感设计
💻 JAVA~38~
字号:
package udpserver;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.util.*;import java.io.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class Frame1 extends JFrame {  JPanel contentPane;    byte[] buf =new byte[256];  DatagramPacket packet;  InetAddress address;  int port;  String dString=null;  private DatagramSocket socket=null;  Button button1 = new Button();  TextField textField1 = new TextField();  Label label1 = new Label();  //Construct the frame  public Frame1() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();      socket=new DatagramSocket();      textField1.setText(String.valueOf(socket.getLocalPort()));    }    catch(Exception e) {      e.printStackTrace();    }  }  //Component initialization  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    button1.setLabel("开始监听");    button1.setBounds(new Rectangle(111, 183, 141, 43));    button1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        button1_actionPerformed(e);      }    });    contentPane.setLayout(null);    this.setSize(new Dimension(400, 300));    this.setTitle("UDP服务器");    textField1.setText("");    textField1.setBounds(new Rectangle(78, 101, 208, 31));    label1.setText("UDP服务器监听端口号为:");    label1.setBounds(new Rectangle(81, 49, 188, 29));    contentPane.add(label1, null);    contentPane.add(textField1, null);    contentPane.add(button1, null);  }  //Overridden so we can exit when window is closed  protected void processWindowEvent(WindowEvent e) {    super.processWindowEvent(e);    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  public void run()  {  if(socket==null)  return;  while(true)  {  try{  packet=new DatagramPacket(buf,256);  socket.receive(packet);  address=packet.getAddress();  port=packet.getPort();  dString= new String("Information form UDP Server");  dString.getBytes(0,dString.length(),buf,0);  packet=new DatagramPacket(buf,buf.length,address,port);  socket.send(packet);  finalize();  }  catch(IOException e)  {  System.err.println("IO异常:"+e);  e.printStackTrace();  }  }  }  protected void finalize()  {  if(socket!=null){  socket.close();  socket=null;  textField1.setText("成功关闭服务器端口!");  }  }  void button1_actionPerformed(ActionEvent e) {      run();  }  }

⌨️ 快捷键说明

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