2个Java聊天小程序

2个Java聊天小程序,第1张

import java.awt.*

import javax.swing.*

import java.awt.event.*

import java.net.*

import java.io.*

class MyChatter extends JFrame {

private JLabel jLName = new JLabel()

private JTextField jTName = new JTextField()

private JLabel jLSendMss = new JLabel()

private JLabel jLReceiveMss = new JLabel()

private JButton jBSend = new JButton()

private JButton jBClear = new JButton()

private JScrollPane jScrollPane1 = new JScrollPane()

private JTextPane jTSendMss = new JTextPane()

private JScrollPane jScrollPane2 = new JScrollPane()

private JTextPane jTReceiveMss = new JTextPane()

//窗口适配器

private MyWindowAdapter mwa=null

//通讯用成员变量

private byte[] receiveBuf=new byte[1000]

private byte[] sendBuf=null

private DatagramSocket datagramServer=null

private DatagramSocket datagramClient=null

private DatagramPacket receivePacket=null

private DatagramPacket sendPacket=null

private static final int PORT=5000

private InetAddress inetAddr=null

private Server server=null

private String machineName=null

private String sendMss=null

class Server extends Thread{

public Server(){

start()

}

public void run(){

while(true){

try{

//System.out.println(datagramServer)

datagramServer.receive(receivePacket)

displayMss()

}

catch(Exception e){

e.printStackTrace()

}

}

}

}

class MyWindowAdapter extends WindowAdapter{

public void windowClosing(WindowEvent e) {

close()

System.exit(0)

}

}

class MyKeyAdapter extends KeyAdapter{

public void keyPressed(KeyEvent e){

//System.out.println(e.getKeyCode())

if(e.isShiftDown()&&e.getKeyCode()==10){

sendReady()

}

}

}

private void sendReady(){

if(!checkMachineName()){

return

}

if(!checkSendMss()){

return

}

if(sendMss()){

JOptionPane.showMessageDialog(null,"发送消息成功!","提示",JOptionPane.ERROR_MESSAGE)

}

else{

JOptionPane.showMessageDialog(null,"发送消息失败!","提示",JOptionPane.ERROR_MESSAGE)

}

}

private boolean sendMss(){

try{

sendPacket=toDatagram(sendMss,inetAddr,PORT)

datagramClient.send(sendPacket)

}

catch(Exception e){

return false

}

return true

}

private DatagramPacket toDatagram(String s,InetAddress destIA,int destPort){

//sendBuf=new byte[s.length()+1]

//s.getBytes(0,s.length(),sendBuf,0)

try{

sendBuf=s.getBytes("GB2312")

}

catch(Exception e){

}

return new DatagramPacket(sendBuf,sendBuf.length,destIA,destPort)

}

private boolean checkMachineName(){

machineName=jTName.getText()

if(machineName.length()==0){

JOptionPane.showMessageDialog(null,"请输入机器名!","警告",JOptionPane.ERROR_MESSAGE)

return false

}

try{

inetAddr=InetAddress.getByName(machineName)

}

catch(UnknownHostException e){

JOptionPane.showMessageDialog(null,"不能识别的机器名或者机器不存在!","警告",JOptionPane.ERROR_MESSAGE)

return false

}

return true

}

private boolean checkSendMss(){

sendMss=jTSendMss.getText()

if(sendMss.length()==0){

JOptionPane.showMessageDialog(null,"要发送的消息不能为空!","警告",JOptionPane.ERROR_MESSAGE)

return false

}

return true

}

private void displayMss(){

String mss=null

String tmp=jTReceiveMss.getText()

try{

mss=new String(receivePacket.getData(),0,receivePacket.getLength(),"GB2312")

}

catch(Exception e){

}

jTReceiveMss.setText(tmp+mss+"\n")

}

private void close(){

}

private void init() throws IOException{

mwa=new MyWindowAdapter()

datagramServer=new DatagramSocket(PORT)

datagramClient=new DatagramSocket()

receivePacket=new DatagramPacket(receiveBuf,receiveBuf.length)

jTSendMss.addKeyListener(new MyKeyAdapter())

server=new Server()

}

public MyChatter() throws HeadlessException {

try {

jbInit()

init()

}

catch(Exception e) {

e.printStackTrace()

}

}

public static void main(String[] args) throws HeadlessException {

MyChatter myChatter1 = new MyChatter()

myChatter1.addWindowListener(myChatter1.mwa)

myChatter1.setSize(400,410)

myChatter1.setVisible(true)

}

private void jbInit() throws Exception {

jLName.setText("机器名或IP地址:")

jLName.setBounds(new Rectangle(15, 7, 108, 18))

this.getContentPane().setLayout(null)

jTName.setBounds(new Rectangle(13, 27, 368, 22))

jLSendMss.setText("发送消息:")

jLSendMss.setBounds(new Rectangle(12, 54, 92, 20))

jLReceiveMss.setText("接收消息:")

jLReceiveMss.setBounds(new Rectangle(12, 208, 86, 19))

jBSend.setBounds(new Rectangle(97, 178, 68, 19))

jBSend.setText("发送")

jBSend.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jBSend_actionPerformed(e)

}

})

jBClear.setText("清空")

jBClear.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jBClear_actionPerformed(e)

}

})

jBClear.setBounds(new Rectangle(224, 179, 68, 19))

this.setResizable(false)

this.setTitle("张辉的聊天程序")

jScrollPane1.setBounds(new Rectangle(13, 77, 368, 90))

jScrollPane2.setBounds(new Rectangle(13, 228, 368, 145))

jTReceiveMss.setEditable(false)

this.getContentPane().add(jLName, null)

this.getContentPane().add(jTName, null)

this.getContentPane().add(jLSendMss, null)

this.getContentPane().add(jLReceiveMss, null)

this.getContentPane().add(jBSend, null)

this.getContentPane().add(jBClear, null)

this.getContentPane().add(jScrollPane1, null)

this.getContentPane().add(jScrollPane2, null)

jScrollPane2.getViewport().add(jTReceiveMss, null)

jScrollPane1.getViewport().add(jTSendMss, null)

}

void jBClear_actionPerformed(ActionEvent e) {

jTSendMss.setText("")

}

void jBSend_actionPerformed(ActionEvent e) {

sendReady()

}

}

绝对能用,OK!给分!!!

给你一个简单的实现吧拍饥,注意一戚贺慎定要高敬先运行MyServer.java

//MyCilent.java

import java.io.*

import java.net.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class MyClient extends JFrame implements ActionListener{

JTextField tf

JTextArea tx

JButton bt

PrintWriter out

public MyClient(){

tf=new JTextField(20)

tx=new JTextArea()

tx.setLineWrap(true)

tx.setWrapStyleWord(true)

JPanel pan=new JPanel()

JScrollPane jsp=new JScrollPane(tx)

add(jsp,"Center")

bt=new JButton("SEND")

bt.addActionListener(this)

pan.add(tf)

pan.add(bt)

add(pan,"South")

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

setTitle("THE CLIENT")

setSize(400,300)

setVisible(true)

try{

Socket socket=new Socket("127.0.0.1",1680)

out=new PrintWriter(socket.getOutputStream(),true)

InputStreamReader in = new InputStreamReader(socket.getInputStream())

BufferedReader sin=new BufferedReader(in)

String s

while(true){

s=sin.readLine()

tx.append("#Server Said#: "+s+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

tx.append("@Client Said@: "+tf.getText()+"\n")

out.println(tf.getText())

tf.setText("")

}

}

public static void main(String[] args){

MyClient mct = new MyClient()

}

}

//MyServer.java

import java.io.*

import java.net.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class MyServer extends JFrame implements ActionListener{

JTextField tf

JTextArea tx

JButton bt

JScrollPane jsp

JPanel pan

PrintWriter out

public MyServer(){

tx=new JTextArea()

tx.setLineWrap(true)

tx.setWrapStyleWord(true)

jsp=new JScrollPane(tx)

tf=new JTextField(20)

bt=new JButton("SEND")

bt.addActionListener(this)

pan=new JPanel()

pan.add(tf)

pan.add(bt)

add(pan,"South")

add(jsp,"Center")

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

setTitle("THE SERVER")

setSize(400,300)

setVisible(true)

try{

ServerSocket server = new ServerSocket(1680)

Socket socket = server.accept()

InputStreamReader in = new InputStreamReader(socket.getInputStream())

BufferedReader sin=new BufferedReader(in)

out=new PrintWriter(socket.getOutputStream(),true)

while(true){

String s=sin.readLine()

tx.append("@Client Said@: "+s+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

String st = tf.getText()

tx.append("#Server Said#: "+st+"\n")

out.println(st)

tf.setText("")

}

}

public static void main(String[] args){

MyServer msr = new MyServer()

}

}

不知核锋手基档道你为什么要用这个 流 DataInputStream sin = new DataInputStream(System.in)

你程序没反应是你读的时候一改嫌直阻塞着。你用 BufferedReader试试 肯定有反应。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12510418.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存