java

位置:IT落伍者 >> java >> 浏览文章

java聊天窗口的实现


发布日期:2020年05月20日
 
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

}

}

               

上一篇:java对word、excel、pdf等操作综合文章

下一篇:Springmvc的开发流程--附带实例