import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import javax.swing.*;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
//import javax.swing.JScrollPane;
public class UDF1 extends JFrame implements Runnable{
Receive r;
Send s;
JLabel jip;
JLabel jipw;
JTextArea messageAll;
//JScrollPane jsp1;
Object o=new Object();
UDF1()
{
this.setLayout(null);
//jsp1 = new JScrollPane();
//jsp1.setVisible(false);
//this.getContentPane().add("center",jsp1);
messageAll = new JTextArea();
messageAll.setBounds(15,15,260,250);
//jsp1.getViewport().add(messageAll);
//this.add(jsp1);
this.add(messageAll);
r=new Receive(o, messageAll);
s=new Send(o, messageAll);
jip =new JLabel("ipַ:");
jip.setBounds(15, 270, 60, 20);
this.add(jip);
s.ipAddress.setBounds(65 ,270, 140, 20);
this.add(s.ipAddress);
s.send.setBounds(210,270,80,20);
this.add(s.send);
jipw=new JLabel("Ϣ");
jipw.setBounds(15, 300, 60, 50);
this.add(jipw);
s.messageSelf =new JTextArea();
s.messageSelf.setBounds(65, 300, 200, 50);
this.add(s.messageSelf);
this.setSize(300,400);
this.setLocation(300,300);
this.setResizable(true);
this.setVisible(true);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
}
public void run()
{
r.start();
s.start();
}
public static void main(String[] args) throws IOException
{
UDF1 f=new UDF1();
f.setResizable(false);
f.setTitle("7--Ҷ--ʵʱͨ");
f.getContentPane().setBackground(Color.getHSBColor(125,268,145));
(new Thread(f)).start();
}
}
class Receive extends Thread
{
private DatagramPacket dp;
DatagramSocket ds;
InetAddress ip;
static final int port=8866;
JTextArea messageAll;
Object o;
boolean first=false;
Receive(Object o, JTextArea messageAll)
{
this.o=o;
this.messageAll = messageAll;
try {
ds=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
public void run()
{
while(true)
{
try {
byte []buf=new byte[100];
dp=new DatagramPacket(buf ,buf.length);
if(!first)
{
messageAll.setText("ͨſʼ!...");
messageAll.setEditable(true);
//messageAll.setBackground(Color.cyan);
first=true;
}
ds.receive(dp);
String s=new String(buf,0,dp.getLength());
messageAll.append('\n'+"Է˵"+s);
} catch (IOException e)
{
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
class Send extends Thread implements ActionListener
{
private DatagramPacket dp;
DatagramSocket ds;
InetAddress ip;
static final int port=8866;
String sendMessage="";
JTextArea messageSelf;
public String getIP;
Object o;
JButton send;
boolean flag=false;
JTextField ipAddress;
JTextArea messageAll;
Send(Object o, JTextArea messageAll)
{
this.o=o;
this.messageAll = messageAll;
send=new JButton("");
//send.setBackground(Color.orange);
send.addActionListener(this);
ipAddress=new JTextField();
try {
ds=new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
}
public void run()
{
while(true)
{
try
{
if((!"".equals(sendMessage.trim()))&&flag)
{
ip=InetAddress.getByName(getIP);
dp=new DatagramPacket(sendMessage.getBytes() ,sendMessage.length(),ip,port);
ds.send(dp);
messageAll.append("\nҶԶԷ˵:"+sendMessage);
}
flag=false;
} catch (IOException e)
{
e.printStackTrace();
}
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
if("".equals(ipAddress.getText().trim()))
{
int TYPE=JOptionPane.WARNING_MESSAGE;
JOptionPane.showMessageDialog(send, "IPַ"," 棡",TYPE);
}
else
{
flag=true;
sendMessage=messageSelf.getText().trim();
//System.out.println(sendMessage);
getIP=ipAddress.getText().trim();
messageSelf.setText("");
}
}
}