熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java聊天窗口的實現

2022-06-13   來源: Java核心技術 

  編寫一數據報通信程序實現簡單的聊天功能

  

  聊天內容輸入文本分別為當前聊天的歷史信息和當前要傳送出去的聊天文本確定清空退出三個按鈕分別實現發送當前聊天文本清空當前聊天文本和退出系統的功能import javaawtFont;

  import javaawteventActionEvent;

  import javaawteventActionListener;

  import javaawteventWindowEvent;

  import javaawteventWindowListener;

  import DatagramPacket;

  import DatagramSocket;

  import InetAddress;

  import SocketException;

  import javaxswingJButton;

  import javaxswingJFrame;

  import javaxswingJLabel;

  import javaxswingJScrollBar;

  import javaxswingJScrollPane;

  import javaxswingJTextArea;

  import javaxswingJTextField;

  public class Frame extends JFrame implements WindowListener{

  private JTextArea text;

  private JTextField ipText;

  private JTextField sendText;

  private JButton button;

  private JButton button;

  private JButton button;

  private DatagramSocket socket;

  private JScrollBar vsBar;

  public Frame(){

  setTitle(聊天器);

  setBounds( );

  text=new JTextArea();

  textsetEditable(true);

  setLayout(null);

  JScrollPane textPanel = new JScrollPane(text);

  vsBar = textPanelgetVerticalScrollBar();

  textPanelsetBounds( );

  getContentPane()add(textPanel);

  JLabel label=new JLabel(請輸入對方IP);

  labelsetFont(new Font(FontBOLD));

  labelsetBounds( );

  getContentPane()add(label);

  ipText = new JTextField();

  ipTextsetBounds( );

  getContentPane()add(ipText);

  button=new JButton();

  buttonsetText(確定);

  buttonsetBounds( );

  buttonsetFont(new Font(FontBOLD));

  getContentPane()add(button);

  buttonaddActionListener(new send());

  button=new JButton(清空);

  buttonsetBounds( );

  buttonsetFont(new Font(FontBOLD));

  getContentPane()add(button);

  buttonaddActionListener(new clear());

  button=new JButton(退出);

  buttonsetBounds( );

  buttonsetFont(new Font(FontBOLD));

  getContentPane()add(button);

  buttonaddActionListener(new exit());

  thisaddWindowListener(this);

  sendText = new JTextField();

  sendTextsetBounds( );

  getContentPane()add(sendText);

  //server();

  pack();

  setVisible(true);

  }

  class send implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  try{

  String ip=ipTextgetText();

  InetAddress address=InetAddressgetByName(ip);

  byte[] data=sendTextgetText()getBytes();

  DatagramPacket dp=new DatagramPacket(datadatalengthaddress);

  String myip=InetAddressgetLocalHost()getHostAddress();

  textappend(myip+:\n+sendTextgetText()+\n);

  socketsend(dp);

  sendTextsetText(null);

  }catch(Exception e){

  Systemoutprintln(e);

  }

  }

  }

  class clear implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  textsetText();

  }

  }

  class exit implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  Systemexit();

  }

  }

  private void server() {

  try {

  socket=new DatagramSocket();

  byte[] buf=new byte[];

  final DatagramPacket dp=new DatagramPacket(bufbuflength);

  Runnable runnable=new Runnable(){

  public void run(){

  while(true){

  try{

  Threadsleep();

  socketreceive(dp);

  String message=new String(dpgetData()dpgetLength());

  String ip=dpgetAddress()getHostAddress();

  if(!InetAddressgetLocalHost()getHostAddress()equals(ip))

  textappend(ip+:\n+message+\n);

  }catch(Exception e){

  Systemoutprintln(e);

  }

  }

  }

  };

  new Thread(runnable)start();

  } catch (SocketException e) {

  eprintStackTrace();

  }

  }

  public static void main(String[] args) {

  Frame frame=new Frame();

  }

  public void windowActivated(WindowEvent e) {

  // TODO Autogenerated method stub

  }

  public void windowClosed(WindowEvent e) {

  // TODO Autogenerated method stub

  }

  public void windowClosing(WindowEvent e) {

  // TODO Autogenerated method stub

  Systemexit();

  }

  public void windowDeactivated(WindowEvent e) {

  // TODO Autogenerated method stub

  }

  public void windowDeiconified(WindowEvent e) {

  // TODO Autogenerated method stub

  }

  public void windowIconified(WindowEvent e) {

  // TODO Autogenerated method stub

  }

  public void windowOpened(WindowEvent e) {

  // TODO Autogenerated method stub

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25580.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.