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

📄 frame1.java~58~

📁 jBuilder实现的udp多线程、多用户聊天工具
💻 JAVA~58~
字号:
package myoicq;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.io.*;
import java.net.*;
import java.applet.Applet;

import java.util.*;


/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Frame1 extends JFrame {
  JPanel contentPane;
  JSplitPane jSplitPane1 = new JSplitPane();
  JPanel jPanel1 = new JPanel();
  public JTextArea jTextArea1 = new JTextArea();
  XYLayout xYLayout1 = new XYLayout();
  JSplitPane jSplitPane2 = new JSplitPane();
  JSplitPane jSplitPane3 = new JSplitPane();
  JLabel jLabel1 = new JLabel();
 public JTextField jTextField1 = new JTextField();
  public JTextArea jTextArea2 = new JTextArea();
  XYLayout xYLayout2 = new XYLayout();
  JSplitPane jSplitPane4 = new JSplitPane();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();

  Thread send = new Thread(new Sender());


  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception {
    contentPane = (JPanel)this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("服务器端");
    jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
    jSplitPane1.setPreferredSize(new Dimension(400, 180));
    jSplitPane1.setDividerSize(2);
    jSplitPane1.setLastDividerLocation(150);
    jSplitPane1.setResizeWeight(0.0);
    contentPane.setPreferredSize(new Dimension(400, 180));
    jTextArea1.setPreferredSize(new Dimension(400, 120));
    jTextArea1.setText("");

    jSplitPane2.setPreferredSize(new Dimension(400, 30));
    jSplitPane2.setDividerSize(5);
    jSplitPane3.setPreferredSize(new Dimension(300, 30));
    jSplitPane3.setDividerSize(5);
    jLabel1.setPreferredSize(new Dimension(50, 30));
    jLabel1.setText("To:");
    jTextField1.setPreferredSize(new Dimension(250, 30));
    jTextField1.setSelectionStart(11);
    jTextField1.setText("");
    jTextArea2.setOpaque(true);
    jTextArea2.setRequestFocusEnabled(false);
    jTextArea2.setEditable(false);
    jTextArea2.setText("");
      jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    jButton1.setText("发送");
    jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
    jButton2.setText("退出");
    jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
    jSplitPane4.setPreferredSize(new Dimension(150, 27));
    contentPane.add(jSplitPane1, new XYConstraints(0, 0, 400, 180));
    jSplitPane1.add(jSplitPane2, JSplitPane.BOTTOM);
    jSplitPane1.add(jTextArea2, JSplitPane.TOP);
    jSplitPane2.add(jSplitPane3, JSplitPane.LEFT);
    jSplitPane3.add(jLabel1, JSplitPane.LEFT);
    jSplitPane3.add(jTextField1, JSplitPane.RIGHT);
    jSplitPane2.add(jSplitPane4, JSplitPane.RIGHT);
    jSplitPane4.add(jButton1, JSplitPane.LEFT);
    jSplitPane4.add(jButton2, JSplitPane.RIGHT);

    contentPane.add(jPanel1, new XYConstraints(0, 170, 400, -1));
    jPanel1.add(jTextArea1, null);
    jSplitPane1.setDividerLocation(150);
    jSplitPane2.setDividerLocation(220);
    jSplitPane3.setDividerLocation(30);
    this.setTitle("ShiLei's oicq");
    jSplitPane4.setDividerLocation(85);

    Thread receive =   new Thread(new Receiver());
      receive.start();

    // if (receive.isAlive())  System.out.print("ok");
  }

  //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);
    }
  }


    class Receiver implements Runnable{
      DatagramSocket socketObj;
    //  DatagramSocket dataSocket;
      String str="";
      byte[] buf=new byte[256];

      public void run(){
        try{
         this.go();
            }catch(Exception e){}
         }

      public void go()throws IOException {

        while(true){
        synchronized (str) {
          socketObj = new DatagramSocket(3325);
          DatagramPacket dataPacket=new DatagramPacket(buf,buf.length);
                /*接收数据报*/
                socketObj.receive(dataPacket);

                /*将获得的数据赋给字符串变量receiveStr*/
                String receiveStr=new String(dataPacket.getData());
                InetAddress ipAddress=dataPacket.getAddress();
               String address = ipAddress.getHostName();
                //String address = InetAddress.toString(ipAddress);
               jTextArea2.append("From "+address+":"+receiveStr);
               System.out.println(receiveStr);
                /*输出得到的数据信息*/
              //  System.out.println("Received data:"+receiveStr);
                /*将DatagramSocket关闭*/
                socketObj.close();

        }
        }
      }

    }

 class Sender implements Runnable{
   String str1 = "";
   public void run(){
     try{
       this. go();
         }catch(IOException d){}
    }

  public void go() throws IOException{
   synchronized(str1){
   /*声明DatagramSocket对象,用来发送和接收数据*/
                DatagramSocket dataSocket=new DatagramSocket();
                /*声明一个byte数组,用来存放数据报的数据*/
                byte[] buf=new byte[256];
                System.out.print("ok");
                String tempStr = jTextArea1.getText();
                buf = tempStr.getBytes();
              //  tempStr = JTextArea1.getText();
                /*下面的语句获得目的地的IP地址*/
                InetAddress address=InetAddress.getByName(jTextField1.getText());
                /*声明一个DatagramPacket对象,参数分别给出发送数据的缓冲区、目的地的IP地址和端口号*/
                DatagramPacket dataPacket=new DatagramPacket(buf,buf.length,address,3325);
                  /*将数据报发送出去*/
                  dataSocket.send(dataPacket);
                  jTextArea2.append("you to "+jTextField1.getText()+":"+jTextArea1.getText()+"\n");
                  dataSocket.close();
                }
 }

 }

  void jButton1_actionPerformed(ActionEvent e) {

  Thread send =  new Thread(new Sender());
  send.start();

}

  void jButton2_actionPerformed(ActionEvent e) {
  // this.addWindowListener(new WindowAdapter());

  }

  }


class Frame1_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton1_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}

class Frame1_jButton2_actionAdapter implements java.awt.event.ActionListener {
  Frame1 adaptee;

  Frame1_jButton2_actionAdapter(Frame1 adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton2_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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