예제 #1
0
  void showWarning(Frame frame, String warning) {
    warningDialog = new Dialog(frame, "警告", true);
    warningDialog.setSize(200, 100);
    warningDialog.setLayout(new FlowLayout());
    warningDialog.setResizable(false);
    warningDialog.setLocationRelativeTo(frame);

    warningText = new Label(warning);
    warningButton = new Button("确认");

    warningDialog.add(warningText);
    warningDialog.add(warningButton);

    warningDialog.addWindowListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            warningDialog.setVisible(false);
            warningDialog.dispose();
          }
        });

    warningButton.addKeyListener(
        new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            int keyCode = e.getKeyCode();

            if (keyCode == KeyEvent.VK_ENTER) {
              warningDialog.setVisible(false);
              warningDialog.dispose();
            }
          }
        });

    warningButton.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            warningDialog.setVisible(false);
            warningDialog.dispose();
          }
        });

    warningDialog.setVisible(true);
  }
  /* DESCRIPTION
   *	Connect button event method,
   *  Call the connect method
   * ARGUMENTS
   *	ip - Peer IP variable, integer
   *	port - Peer Port variable, integer
   *  dlg - Warning dialog, Dialog
   *  msg - Warning message, Label
   *  ok - confirm button on the warning dialog
   */
  protected void connect_btnMouseClicked(MouseEvent evt) {

    ip = ip_text.getText();
    port = Integer.parseInt(port_text.getText());

    final Dialog dlg = new Dialog(this, "Warning!");
    dlg.setSize(300, 100);
    Label msg = new Label("Input the correct Ip address and Port number.", Label.CENTER);
    Button ok = new Button("OK");

    dlg.setLayout(new FlowLayout());
    dlg.add(msg);
    dlg.add(ok);

    ok.addMouseListener(
        new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            dlg.dispose();
          }
        });

    // if user didn't a peer ip, it shows alarm dialog
    if (ip.equals("")) {
      dlg.setVisible(true);
    }

    // Call the connect method
    else {

      try {
        connect();

      } catch (Exception e) {

        dlg.setVisible(true);
        System.out.println(e);
      }
    }
  }
  /* DESCRIPTION
   *	Send button event method,
   *  Send the data to socket
   * ARGUMENTS
   *	sendstr - send data, String
   *  hexstr -  send data converted to hex, String
   *  dlg - Warning dialog, Dialog
   *  msg - Warning message, Label
   *  ok - confirm button on the warning dialog
   *  pw - output stream
   */
  protected void send_btnMouseClicked(MouseEvent evt) {

    String sendstr = "";
    String hexstr = "";

    // if the user click the send button when the socket is close, it shows warning dialog
    if (sock == null) {

      final Dialog dlg = new Dialog(this, "Warning!");
      dlg.setSize(300, 100);
      Label msg = new Label("Connect the socket, first.", Label.CENTER);
      Button ok = new Button("OK");

      dlg.setLayout(new FlowLayout());
      dlg.add(msg);
      dlg.add(ok);

      ok.addMouseListener(
          new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
              dlg.dispose();
            }
          });

      dlg.setVisible(true);
    }

    // send the data to socket
    else {

      sendstr = send_text.getText();

      if (sendstr.equals("") && cr_check.isSelected() == false && lf_check.isSelected() == false) {

        final Dialog dlg = new Dialog(this, "Warning!");
        dlg.setSize(300, 100);
        Label msg = new Label("Insert the message", Label.CENTER);
        Button ok = new Button("OK");

        dlg.setLayout(new FlowLayout());
        dlg.add(msg);
        dlg.add(ok);

        ok.addMouseListener(
            new MouseAdapter() {
              public void mouseClicked(MouseEvent e) {
                dlg.dispose();
              }
            });

        dlg.setVisible(true);
      } else {
        pw.print(sendstr);

        if (cr_check.isSelected()) {
          pw.print("\r");
          sendstr = sendstr + "\r";
        }
        if (lf_check.isSelected()) {
          pw.print("\n");
          sendstr = sendstr + "\n";
        }
        pw.flush();

        hexstr = StringtoHex(sendstr);

        data.append("Send data : ");
        data.append(hexstr + " | ");
        data.append(sendstr);
        data.append("\n");

        send_text.setText("");
        sendstr = "";
      }
    }
  }