java聊天視窗的實現

來源:互聯網
上載者:User

java聊天視窗的實現
編寫一資料報通訊程式,實現簡單的聊天功能。


  “聊天內容”和“輸入文本”分別為當前聊天的曆史資訊和當前要傳送出去的聊天文本。“確定”、“清空”、“退出”三個按鈕分別實現發送當前聊天文本、清空當前聊天文本和退出系統的功能。import java.awt.Font;

  import java.awt.event.ActionEvent;

  import java.awt.event.ActionListener;

  import java.awt.event.WindowEvent;

  import java.awt.event.WindowListener;

  import java.net.DatagramPacket;

  import java.net.DatagramSocket;

  import java.net.InetAddress;

  import java.net.SocketException;

  import javax.swing.JButton;

  import javax.swing.JFrame;

  import javax.swing.JLabel;

  import javax.swing.JScrollBar;

  import javax.swing.JScrollPane;

  import javax.swing.JTextArea;

  import javax.swing.JTextField;

  public class Frame extends JFrame implements WindowListener{

  private JTextArea text;

  private JTextField ipText;

  private JTextField sendText;

  private JButton button;

  private JButton button1;

  private JButton button2;

  private DatagramSocket socket;

  private JScrollBar vsBar;
  
   public Frame(){
  setTitle("聊天器");

  setBounds(100, 150,481, 371);

  text=new JTextArea();

  text.setEditable(true);

  setLayout(null);

  JScrollPane textPanel = new JScrollPane(text);

  vsBar = textPanel.getVerticalScrollBar();

  textPanel.setBounds(10,10, 320, 240);

  getContentPane().add(textPanel);

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

  label.setFont(new Font("",Font.BOLD,14));

  label.setBounds(342, 24, 110, 24);

  getContentPane().add(label);

  ipText = new JTextField();

  ipText.setBounds(352, 54, 111, 31);

  getContentPane().add(ipText);

  button=new JButton();

  button.setText("確定");

  button.setBounds(363, 135, 85, 47);

  button.setFont(new Font("",Font.BOLD,23));

  getContentPane().add(button);

  button.addActionListener(new send());

  button1=new JButton("清空");

  button1.setBounds(363, 200, 85, 47);

  button1.setFont(new Font("",Font.BOLD,23));

  getContentPane().add(button1);

  button1.addActionListener(new clear());

  button2=new JButton("退出");

  button2.setBounds(363, 260, 85, 47);

  button2.setFont(new Font("",Font.BOLD,23));

  getContentPane().add(button2);

  button2.addActionListener(new exit());

  this.addWindowListener(this);

  sendText = new JTextField();

  sendText.setBounds(10, 260, 320, 47);

  getContentPane().add(sendText);

  //server();

  pack();

  setVisible(true);

  }
   class send implements ActionListener{
  public void actionPerformed(ActionEvent e) {

  try{

  String ip=ipText.getText();

  InetAddress address=InetAddress.getByName(ip);

  byte[] data=sendText.getText().getBytes();

  DatagramPacket dp=new DatagramPacket(data,data.length,address,9527);

  String myip=InetAddress.getLocalHost().getHostAddress();

  text.append(myip+":n"+sendText.getText()+"n");

  socket.send(dp);

  sendText.setText(null);

  }catch(Exception e1){

  System.out.println(e1);

  }

  }

  }

  class clear implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  text.setText("");

  }

  }

  class exit implements ActionListener{

  public void actionPerformed(ActionEvent e) {

  System.exit(0);

  }

  }

  private void server() {

  try {

  socket=new DatagramSocket(9527);

  byte[] buf=new byte[1024];

  final DatagramPacket dp1=new DatagramPacket(buf,buf.length);

  Runnable runnable=new Runnable(){

  public void run(){

  while(true){

  try{

  Thread.sleep(100);

  socket.receive(dp1);

  String message=new String(dp1.getData(),0,dp1.getLength());

  String ip=dp1.getAddress().getHostAddress();

  if(!InetAddress.getLocalHost().getHostAddress().equals(ip))

  text.append(ip+":n"+message+"n");

  }catch(Exception e){

  System.out.println(e);

  }

  }

  }

  };
   new Thread(runnable).start();
  } catch (SocketException e1) {

  e1.printStackTrace();

  }

  }

  public static void main(String[] args) {

  Frame frame=new Frame();

  }

  public void windowActivated(WindowEvent e) {

  // TODO Auto-generated method stub

  }

  public void windowClosed(WindowEvent e) {

  // TODO Auto-generated method stub

  }

  public void windowClosing(WindowEvent e) {

  // TODO Auto-generated method stub

  System.exit(0);

  }

  public void windowDeactivated(WindowEvent e) {

  // TODO Auto-generated method stub

  }

  public void windowDeiconified(WindowEvent e) {

  // TODO Auto-generated method stub

  }

  public void windowIconified(WindowEvent e) {

  // TODO Auto-generated method stub

  }

  public void windowOpened(WindowEvent e) {

  // TODO Auto-generated method stub

  }

  }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.