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

📄 udptimeclientframe.java

📁 Java实例入门
💻 JAVA
字号:
package udptimeclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.net.*;import java.util.*;import java.io.*;/** * Title:        时钟服务测试程序 * Description:  这是一个使用时钟服务的客户端程序 * Copyright:    Copyright (c) 2001 * Company: * @author * @version 1.0 */public class UdpTimeClientFrame extends JFrame {  JPanel contentPane;  BorderLayout borderLayout1 = new BorderLayout();  JPanel jPanelCmd = new JPanel();  JButton command = new JButton();  JPanel jPanelShowTime = new JPanel();  JLabel jLabelTime = new JLabel();  BorderLayout borderLayout2 = new BorderLayout();  JLabel jLabel1 = new JLabel();  JTextField txtServAddr = new JTextField();  JLabel jLabel2 = new JLabel();  JTextField txtServPort = new JTextField();  /**Construct the frame*/  public UdpTimeClientFrame() {    enableEvents(AWTEvent.WINDOW_EVENT_MASK);    try {      jbInit();    }    catch(Exception e) {      e.printStackTrace();    }  }  /**Component initialization*/  private void jbInit() throws Exception  {    //setIconImage(Toolkit.getDefaultToolkit().createImage(UdpTimeClientFrame.class.getResource("[Your Icon]")));    contentPane = (JPanel) this.getContentPane();    contentPane.setLayout(borderLayout1);    this.setSize(new Dimension(459, 99));    this.setTitle("时钟服务测试程序");    command.setText("读取时间");    command.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        command_actionPerformed(e);      }    });    jLabelTime.setFont(new java.awt.Font("SansSerif", 1, 20));    jLabelTime.setHorizontalAlignment(SwingConstants.CENTER);    jLabelTime.setText("在此处显示时间值");    jPanelShowTime.setLayout(borderLayout2);    jLabel1.setText("服务器地址");    txtServAddr.setColumns(10);    jLabel2.setText("服务端口号");    txtServPort.setText("13");    txtServPort.setColumns(5);    jPanelCmd.add(jLabel1, null);    jPanelCmd.add(txtServAddr, null);    jPanelCmd.add(jLabel2, null);    jPanelCmd.add(txtServPort, null);    contentPane.add(jPanelCmd, BorderLayout.NORTH);    jPanelCmd.add(command, null);    contentPane.add(jPanelShowTime, BorderLayout.CENTER);    jPanelShowTime.add(jLabelTime, BorderLayout.CENTER);  }  /**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);    }  }  void command_actionPerformed(ActionEvent e) {  byte[]         outBuffer = new byte[1];  byte[]         inBuffer = new byte[512];   try{       String host = txtServAddr.getText().trim() ;       InetAddress hostAddress = InetAddress.getByName(host);       DatagramSocket timeSocket = new DatagramSocket();       // 创建请求和应答缓冲区       DatagramPacket request = new DatagramPacket(outBuffer, outBuffer.length, hostAddress, 13);       DatagramPacket reply = new DatagramPacket(inBuffer, inBuffer.length);       // 发送请求并读取应答       try {             timeSocket.setSoTimeout(5 * 1000);             timeSocket.send(request);             timeSocket.receive(reply);             jLabelTime.setText(new String(inBuffer, 0, reply.getLength()));        } finally {                  timeSocket.close();        }   } catch(Exception ex) {         System.out.println(ex);   }  }}

⌨️ 快捷键说明

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