swt 关于DUP通信,不晓得哪里出问题了。望各位帮忙!!
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 java.text.SimpleDateFormat;
import java.util.Calendar;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
public class Frame
{
DatagramSocket ds ;
private Shell sShell = null;
private Text textArea = null;
private Label label = null;
private Text text = null;
private Label label1 = null;
private Text text1 = null;
private Label label2 = null;
private Button button = null;
Calendar cl=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args)
{
Display display = Display.getDefault();
Frame thisClass = new Frame();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private void createSShell()
{
sShell = new Shell();
sShell.setText("消息处理框C");
sShell.setSize(new Point(563, 371));
sShell.setLayout(null);
textArea = new Text(sShell, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
textArea.setBounds(new Rectangle(29, 34, 490, 185));
textArea.setFont(new Font(Display.getDefault(), "楷体_GB2312", 12, SWT.BOLD));
label = new Label(sShell, SWT.NONE);
label.setBounds(new Rectangle(36, 6, 110, 21));
label.setFont(new Font(Display.getDefault(), "楷体_GB2312", 12, SWT.BOLD));
label.setText("消息显示栏");
text = new Text(sShell, SWT.BORDER);
text.setBounds(new Rectangle(27, 247, 359, 31));
text.setFont(new Font(Display.getDefault(), "新宋体", 10, SWT.BOLD));
label1 = new Label(sShell, SWT.NONE);
label1.setBounds(new Rectangle(1, 222, 85, 21));
label1.setFont(new Font(Display.getDefault(), "楷体_GB2312", 10, SWT.BOLD));
label1.setText("请输入内容:");
text1 = new Text(sShell, SWT.BORDER);
text1.setBounds(new Rectangle(27, 307, 362, 32));
text1.setFont(new Font(Display.getDefault(), "新宋体", 10, SWT.BOLD));
label2 = new Label(sShell, SWT.NONE);
label2.setBounds(new Rectangle(0, 285, 73, 21));
label2.setFont(new Font(Display.getDefault(), "楷体_GB2312", 10, SWT.BOLD));
label2.setText("请输入IP:");
button = new Button(sShell, SWT.NONE);
button.setBounds(new Rectangle(418, 245, 111, 75));
button.setFont(new Font(Display.getDefault(), "楷体_GB2312", 10, SWT.BOLD));
button.setText("发送");
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()
{
@SuppressWarnings("null")
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)
{
DatagramPacket dp=null;
byte buf[];
DatagramSocket ds =null ;
try
{
buf=(text.getText()).getBytes();
dp=new DatagramPacket(buf, buf.length, InetAddress.getByName(text1.getText()),3000);
}
catch (UnknownHostException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
try
{
ds.send(dp);
}
catch (IOException e1)
{
// TODO Auto-generated catch block
System.out.println("数据发送失败!请重试!");
}
textArea.append(text.getText()+" "+"from"+" "+sdf.format(cl.getTime())+" "+dp.getAddress().getHostAddress()+"\n");
text.setText(""); }
});
new Thread(new Runnable()
{
public void run()
{
byte buf[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf, 1024);
while(true)
{
try
{
ds.receive(dp);
if(dp.getLength()>0)
{
String str=new String(buf, 0, dp.getLength());
textArea.append(str+"from"+sdf.format(cl.getTime())+dp.getAddress().getHostAddress()+"\n");
}
}
catch (IOException e)
{ e.printStackTrace();
}
}
}
}).start();
}
}