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

📄 sendmessage.java

📁 网络聊天程序
💻 JAVA
字号:
package Java.ICQ;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class SendMessage extends JDialog 
{
	JPanel panel1 = new JPanel();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();
    JLabel jLabel4 = new JLabel();
    JTextField jTextField1 = new JTextField();
    JTextField jTextField2 = new JTextField();
    JTextArea jTextArea1 = new JTextArea();
    JTextArea jTextArea2 = new JTextArea();
    JButton jButton1 = new JButton();
    JButton jButton2 = new JButton();
    JScrollPane scrollpane = new JScrollPane();
    JScrollPane scrollpane1 = new JScrollPane();
    MainFrm mainFrm;
    Friend  friend;
    byte[] buffer = new byte[1024];
    InetAddress address;
    boolean falg=true;
	public SendMessage(Frame frame,String title,boolean model,MainFrm mainFrm)
	{
		super(frame,title,model);
		this.mainFrm = mainFrm;
		initUI();
		try
		{
	    	initDate();
         	
		}
		catch(Exception e)
		{
			System.out.println("读取好友资料失败");
		}
	
	}
	public void  initUI()
	{
		
	    panel1.setLayout(null);
        panel1.setBounds(new Rectangle(0, 15, 1, 1));
        jLabel1.setText("jLabel1");
        jLabel1.setBounds(new Rectangle(36, 45, 42, 15));
        this.getContentPane().setLayout(null);
        jLabel2.setFont(new java.awt.Font("宋体", Font.PLAIN, 15));
        jLabel2.setText("接收消息:");
        jLabel2.setBounds(new Rectangle(14, 6, 126, 31));
        jLabel3.setText("昵称:");
        jLabel3.setBounds(new Rectangle(19, 41, 53, 24));
        jLabel4.setText("Java QICQ:");
        jLabel4.setBounds(new Rectangle(175, 41, 71, 26));
        jTextField1.setBounds(new Rectangle(76, 41, 94, 24));
        jTextField2.setBounds(new Rectangle(259, 40, 103, 26));
        scrollpane.setBounds(new Rectangle(22, 72, 342, 142));
        jButton1.setBounds(new Rectangle(83, 333, 81, 26));
        jButton1.setText("发送");
        jButton2.setBounds(new Rectangle(241, 333, 87, 27));
        jButton2.setToolTipText("");
        jButton2.setText("取消");
        scrollpane1.setBounds(new Rectangle(23, 221, 342, 103));
        panel1.add(jLabel1);
        this.getContentPane().add(jLabel2);
        this.getContentPane().add(jTextField1);
        this.getContentPane().add(jTextField2);
        this.getContentPane().add(jLabel4);
        this.getContentPane().add(scrollpane);
        this.getContentPane().add(scrollpane1);
        this.getContentPane().add(jButton1);
        this.getContentPane().add(jButton2);
        this.getContentPane().add(jLabel3);
        this.getContentPane().add(panel1, null);
        
        scrollpane.getViewport().add(jTextArea1,null);
        scrollpane1.getViewport().add(jTextArea2,null);
        jTextField1.setEditable(false);
        jTextField2.setEditable(false);
        
        jButton1.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		jButton1_actionPerformed(e);
        	}
        });
        jButton2.addActionListener(new ActionListener()
        {
        	public void actionPerformed(ActionEvent e)
        	{
        		jButton2_actionPerformed(e);
        	}
        });
        
  
	}
	public void initDate()
	{
		int select = mainFrm.listFriend.getSelectedIndex();
		
		System.out.println(mainFrm.vectorList.size());
		friend = (Friend)mainFrm.vectorList.elementAt(select);
		
		System.out.println(friend.friendNickname);
		System.out.println(friend.friendIcqno);
		System.out.println(friend.friendIp);
		
		jTextField1.setText(friend.friendNickname.trim());
		jTextField2.setText(String.valueOf(friend.friendIcqno).trim());
		

		
	}
	public void jButton1_actionPerformed(ActionEvent e)
	{
		
		if(jTextArea2.getText().trim().length()>0)
		{
		  try
		  {
		  	address=InetAddress.getByName(friend.friendIp);
			send();
		  }
		  catch(Exception ee)
		  {
		  	System.out.println("没有找到ip地址");
		  }

		}
		else
		{
		    JOptionPane.showMessageDialog(this,"您发送的消息不能为空","消息对话框",JOptionPane.INFORMATION_MESSAGE);
		}

		
	}
	
	public void jButton2_actionPerformed(ActionEvent e)
	{
		this.dispose();
	}

	public void send()
	{
		
		buffer  = (jTextArea2.getText().trim()+"/oicq"+friend.friendNickname).getBytes();
		try
		{
     	 
	     System.out.println(address);
		    if(friend.friendIp != null)
		    {
		    	DatagramPacket	 data_pack=new DatagramPacket(buffer,buffer.length,address,8888);
		    	mainFrm.mail_data.send(data_pack);
		    	if(falg==true)
		    	{
		    	  jTextArea1.append(friend.friendNickname+"说:\n");
		    	  jTextArea1.append(jTextArea2.getText().trim()+"\n");
		    	  jTextArea2.setText("");
		    	}

		    }
		    else
		    {
		         JOptionPane.showMessageDialog(this,"您的好友不在线,发送消息失败!","消息对话框",JOptionPane.INFORMATION_MESSAGE);
		    }
		}
		catch(Exception ie)
		{ 
			System.out.println("发送消息失败");
		}
	}
}

⌨️ 快捷键说明

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