www.pudn.com > networktools.rar > telnetClientFrame.java


package telnetclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import ipworks.*;

public class telnetClientFrame extends JFrame {
  private JPanel contentPane;
  private TextField textField1 = new TextField();
  private Button button1 = new Button();
  private Label label1 = new Label();
  private TextArea textArea1 = new TextArea();
  private Telnet telnet1 = new Telnet();
  private Button button2 = new Button();

  //Construct the frame
  public telnetClientFrame() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(telnetClientFrame.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    textField1.setBounds(new Rectangle(108, 4, 206, 21));
    contentPane.setLayout(null);
    this.setSize(new Dimension(504, 429));
    this.setTitle("Telnet客户端");
    button1.setLabel("连接");
    button1.setBounds(new Rectangle(323, 3, 72, 23));
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button1_actionPerformed(e);
      }
    });
    label1.setText("输入站点名:");
    label1.setBounds(new Rectangle(20, 4, 80, 21));
    textArea1.setBounds(new Rectangle(6, 35, 478, 338));
    textArea1.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyTyped(KeyEvent e) {
        textArea1_keyTyped(e);
      }
    });
    button2.setEnabled(false);
    button2.setLabel("断开");
    button2.setBounds(new Rectangle(408, 3, 74, 23));
    button2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button2_actionPerformed(e);
      }
    });
    telnet1.addTelnetEventListener(new ipworks.TelnetEventListener() {
      public void command(TelnetCommandEvent e) {
      }
      public void connected(TelnetConnectedEvent e) {
      }
      public void dataIn(TelnetDataInEvent e) {
        telnet1_dataIn(e);
      }
      public void disconnected(TelnetDisconnectedEvent e) {
      }
      public void doDo(TelnetDoDoEvent e) {
        telnet1_doDo(e);
      }
      public void dont(TelnetDontEvent e) {
        telnet1_dont(e);
      }
      public void error(TelnetErrorEvent e) {
      }
      public void readyToSend(TelnetReadyToSendEvent e) {
      }
      public void subOption(TelnetSubOptionEvent e) {
      }
      public void will(TelnetWillEvent e) {
        telnet1_will(e);
      }
      public void wont(TelnetWontEvent e) {
        telnet1_wont(e);
      }
    });
    contentPane.add(textArea1, null);
    contentPane.add(textField1, null);
    contentPane.add(button1, null);
    contentPane.add(label1, null);
    contentPane.add(button2, 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);
    }
  }

  void button1_actionPerformed(ActionEvent e) {
    try
    {
      telnet1.setTimeout(100);              //设置连接到Telnet主机的缺省最大时间
      telnet1.setRemoteHost(textField1.getText()); //设置Telnet主机的名称
      telnet1.setConnected(true);          //连接到Telnet主机
      button2.setEnabled(true);
      button1.setEnabled(false);
    }
    catch (Exception e1)
    {
      System.out.println("Error: " + e1);   //捕捉异常情况
    }
  }

  void button2_actionPerformed(ActionEvent e) {
    try
    {
      telnet1.setConnected(false);        //与Telnet主机断开连接
      textArea1.setText("");
      button1.setEnabled(true);
      button2.setEnabled(false);
    }
    catch(Exception e1)
    {
      System.out.println("Error: " + e1);  //捕捉异常情况
    }
  }

  void telnet1_dataIn(TelnetDataInEvent e) {   //当接收到Telnet主机发送的数据时产生的动作
    textArea1.append(new String(e.text));
  }

  void telnet1_doDo(TelnetDoDoEvent e) {       //当接收到Telnet主机发送的Telnet DO OPTION命令时产生的动作
    try {
      telnet1.setDoOption(e.optionCode);     //向Telnet主机发送一个单字节的Telnet option code以响应Telnet主机发送的命令
    }
    catch (Exception e1) {
      System.out.println("Error: " + e1);      //捕捉异常情况
    }
  }

  void telnet1_will(TelnetWillEvent e) {     //当接收到Telnet主机发送的Telnet WILL OPTION命令时产生的动作
    try {
      telnet1.setWillOption(e.optionCode);    //向Telnet主机发送一个单字节的Telnet option code以响应Telnet主机发送的命令
    }
    catch (Exception e1) {
      System.out.println("Error: " + e1);    //捕捉异常情况
    }
  }

  void telnet1_dont(TelnetDontEvent e) {       //当接收到Telnet主机发送的Telnet DONT OPTION命令时产生的动作
    try {
      telnet1.setDontOption(e.optionCode);     //向Telnet主机发送一个单字节的Telnet option code以响应Telnet主机发送的命令
    }
    catch (Exception e1) {
      System.out.println("Error: " + e1);      //捕捉异常情况
    }
  }

  void telnet1_wont(TelnetWontEvent e) {      //当接收到Telnet主机发送的Telnet WONT OPTION命令时产生的动作
    try {
      telnet1.setWontOption(e.optionCode);     //向Telnet主机发送一个单字节的Telnet option code以响应Telnet主机发送的命令
    }
    catch (Exception e1) {
      System.out.println("Error: " + e1);      //捕捉异常情况
    }
  }

  void textArea1_keyTyped(KeyEvent e) {     //当用户在Telnet文字显示区域敲入字符时产生的动作
    try {
      byte[] toSend = new byte[1];
      toSend[0] = (byte)e.getKeyChar();      //将用户输入的字符转换为字节形式
      telnet1.setDataToSend(toSend);         //向Telnet主机发送用户数据
      e.consume();                           //不产生缺省的按键动作
    }
    catch (Exception e1) {
      System.out.println("Error: " + e1);    //捕捉异常情况
    }
  }
}