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

📄 inputnametextarea.java~11~

📁 局域网聊天系统
💻 JAVA~11~
字号:
package com.soft.Team;

import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.util.Hashtable;
import java.net.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class InputNameTextArea
    extends JFrame implements Runnable {
  JPanel contentPane;
  String name = null;
  XYLayout xYLayout1 = new XYLayout();
  Label label1 = new Label();
  CheckboxGroup group = null;
  TextField nameFile = new TextField();
  Checkbox male = new Checkbox();
  Checkbox female = new Checkbox();
  Button 进入聊天室 = new Button();
  Button 退出聊天室 = new Button();
  Socket socket = null;
  DataInputStream in = null;
  DataOutputStream out = null;
  boolean 能否聊天 = false;
  Hashtable ListTable;
  Thread thread = null;
  public InputNameTextArea(Hashtable listTable) {
    try {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      jbInit(ListTable);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  /**
   * Component initialization.
   *
   * @throws java.lang.Exception
   */
  private void jbInit(Hashtable listTable) throws Exception {
    this.ListTable = ListTable;
    group = new CheckboxGroup();
    contentPane = (JPanel) getContentPane();
    contentPane.setLayout(xYLayout1);
    setSize(new Dimension(400, 300));
    setTitle("登入框架");
    thread = new Thread(this);
    label1.setText("用户昵称");
    male.setForeground(Color.black);
    male.setLabel("男");
    female.setLabel("女");
    进入聊天室.setLabel("进入聊天室");
    进入聊天室.addActionListener(new InputNameTextArea_进入聊天室_actionAdapter(this));
    退出聊天室.setLabel("退出聊天室");
    退出聊天室.addActionListener(new InputNameTextArea_退出聊天室_actionAdapter(this));
    contentPane.add(label1, new XYConstraints(34, 40, 102, 35));
    contentPane.add(nameFile, new XYConstraints(148, 37, 171, 37));
    contentPane.add(male, new XYConstraints(35, 101, 103, 29));
    contentPane.add(female, new XYConstraints(151, 100, 118, 33));
    contentPane.add(进入聊天室, new XYConstraints(34, 166, 191, 35));
    contentPane.add(退出聊天室, new XYConstraints(33, 218, 191, 36));
  }

  public void set能否聊天(boolean b) {
    能否聊天 = b;
  }

  public boolean get能否聊天() {
    return 能否聊天;
  }

  public String getName() {
    return name;
  }

  public void setName(String s) {
    name = s;
  }

  public void setSocketConnection(Socket socket, DataInputStream in,
                                  DataOutputStream out) {

    this.socket = socket;
    this.in = in;
    this.out = out;
    try {
      thread.start();
    }
    catch (Exception e) {
      nameFile.setText("" + e);
    }
  }

  public Socket getSocket() {
    return socket;
  }

  public void 进入聊天室_actionPerformed(ActionEvent e) {

    退出聊天室.setEnabled(true);
    if (能否聊天 == true) {
      nameFile.setText("您正在聊天:" + name);
    }
    else {
      this.setName(nameFile.getText());
      String sex = group.getSelectedCheckbox().getLabel();
      if (socket != null && name != null) {
        try {
          out.writeUTF("姓名:" + name + "性别:" + sex);
        }
        catch (IOException ee) {
          nameFile.setText("没有连通服务器" + ee);
        }
      }
    }

  }

  public void 退出聊天室_actionPerformed(ActionEvent e) {
    try {
      out.writeUTF("用户离开:");
    }
    catch (IOException ee) {
    }
  }

  public void run() {
    String message = null;
    while (true) {
      if (in != null) {
        try {
          message = in.readUTF();
        }
        catch (IOException e) {
          nameFile.setText("和服务器断开" + e);
        }
      }
      if (message.startsWith("可以聊天:")) {
        能否聊天 = true;
        break;
      }
      else if (message.startsWith("聊天者:")) {
        String people = message.substring(message.indexOf(":") + 1);
        ListTable.put(people, people);
      }
      else if (message.startsWith("不可以聊天:")) {
        能否聊天 = false;
        nameFile.setText("该昵称已被占用");
      }
    }
  }

}

class InputNameTextArea_退出聊天室_actionAdapter
    implements ActionListener {
  private InputNameTextArea adaptee;
  InputNameTextArea_退出聊天室_actionAdapter(InputNameTextArea adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.退出聊天室_actionPerformed(e);
  }
}

class InputNameTextArea_进入聊天室_actionAdapter
    implements ActionListener {
  private InputNameTextArea adaptee;
  InputNameTextArea_进入聊天室_actionAdapter(InputNameTextArea adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.进入聊天室_actionPerformed(e);
  }

}

⌨️ 快捷键说明

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