游戏代码部分-------game包------ChatBar .java

来源:互联网 发布:ios旧版手机淘宝 编辑:程序博客网 时间:2024/05/23 01:39
package game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.*;
/**
 * 类ChatBar,扩充Panel,用于玩家的信息交互
 * @author 赵建川
 * @see #ChatBar(NetRead netread)
 * @see #appendStr()
 * @see #sendStr()
 * @see #btnSend_actionPerformed()
 * @see #btnSend_actionPerformed()
 */
public class ChatBar extends Panel {
  private TextArea txtShow=new TextArea();
  private JLabel lblInfo=new JLabel();
  private JTextField txtSend=new JTextField();
  private JButton btnSend=new JButton();
  private Net m_NetRead;
  private Color BackColor=new  Color(80,123,166);
  /**
   * 构造函数 ChatBar
   */
  public ChatBar()
  {
    setBackground(this.BackColor);
    try {
      jbInit();
      myInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 构造函数 ChatBar
   * @param netread 网络接口
   */
  public ChatBar(Net netread)
  {
    m_NetRead=netread;
    setBackground(BackColor);
    try
    {
      jbInit();
      myInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  private void myInit()
  {
  }
  /**
   * ChatBar初始化 jbInit()
   * @throws java.lang.Exception
   */
  private void jbInit() throws Exception
  {
    this.setLayout(null);
    this.setBackground(new Color(80, 123, 166));
    txtShow = new TextArea("", 3, 0, TextArea.SCROLLBARS_VERTICAL_ONLY);
    txtShow.setBackground(BackColor);
    txtShow.setEditable(false);
    txtShow.setBounds(new Rectangle(78, 13, 549, 57));
    lblInfo.setText("发送消息");
    //lblInfo.setText("Message");
    lblInfo.setBounds(new Rectangle(74, 81, 58, 24));
    txtSend.setBackground(BackColor);
    txtSend.setBounds(new Rectangle(135, 78, 409, 25));
    //btnSend.setText("Send") ;
    btnSend.setText("发送") ;
    btnSend.setBackground(BackColor);
    btnSend.setBounds(new Rectangle(552, 78, 75, 26));
    this.add(btnSend, null);
    this.add(txtShow, null);
    this.add(txtSend, null);
    this.add(lblInfo, null);
    btnSend.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        btnSend_actionPerformed(e);
      }
    });
     txtSend.addKeyListener(new java.awt.event.KeyAdapter()
     {
       public void keyPressed(KeyEvent e)
       {
         txtSend_keyPressed(e);
       }
     });
   }//end jbInit()
   /**
    * 发送按钮的Click事件向另一玩家发送消息(如果是对战状态)
    * @param e
    */
   void btnSend_actionPerformed(ActionEvent e)
   {
     String str=txtSend.getText().trim();
     if (!str.equals(""))
     {
       appendStr( "自己:" + str+"/n" );
       sendStr(m_NetRead.TALK+ ":" + str);
       txtSend.setText("");
     }
   }
   void txtSend_keyPressed(KeyEvent e)
   {
     if(e.getKeyCode()==KeyEvent.VK_ENTER)
     {
       String str=txtSend.getText().trim();
       if (!str.equals("") )
       {
         appendStr("自己:" + str+"/n");
         m_NetRead.sendStr(m_NetRead.TALK + ":" + str);
         txtSend.setText("");
       }
     }
   }
   /**
    * 把要发送的信息显示消息在区域
    * @param str String 发送的信息字符串
    */
   public void appendStr(String str)
   {
     txtShow.append(str);
   }
   /**
    *发送消息给对方
    * @param str 发送的字符串
    * @see NetRead#sendStr(String)
    */
   public void sendStr(String str)
   {
     m_NetRead.sendStr(str);
   }
}

原创粉丝点击