67,512
社区成员
发帖
与我相关
我的任务
分享
package communicationClient;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class CommunicationClient extends Applet{
boolean isStandalone=false;
JLabel jlabel1=new JLabel();
JLabel jlabel2=new JLabel();
JTextField jtextfield1=new JTextField();
JTextArea jtextarea1=new JTextArea();
private DatagramSocket sendSocket,receiveSocket;
private DatagramPacket sendPacket,receivePacket;
public String getParameter(String key,String def){
return isStandalone ? System.getProperty(key,def) : (getParameter(key)!=null ? getParameter(key) :def);
}
public void init(){
try{
jbInit();
}catch(Exception e){
e.printStackTrace();
}
}
private void jbInit() throws Exception{
jlabel1.setText("通讯记录 : ");
jlabel1.setBounds(new Rectangle(16,9,68,27));
setLayout(null);
jtextarea1.setText("你好!");
jtextarea1.setBounds(new Rectangle(15,48,349,59));
jlabel2.setText("输入通讯内容:");
jlabel2.setBounds(new Rectangle(17,125,92,37));
jtextfield1.setText("通讯内容:");
jtextfield1.setBounds(new Rectangle (127,129,244,33));
jtextfield1.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
jtextfield1_actionPerformed(e);
}
});
this.add(jlabel1,null);
this.add(jtextarea1,null);
this.add(jlabel2,null);
this.add(jtextfield1,null);
}
public void start(){
this.waitForPackets();
}
public String getAppletInfo(){
return "Applet Infomation";
}
public String[][] getParameterInfo(){
return null;
}
public void waitForPackets(){
try
{
sendSocket=new DatagramSocket();
receiveSocket=new DatagramSocket(8001);
}
catch(SocketException e){
jtextarea1.append("不能打开数据Socket,或数据Socket 无法与指定端口连接!");
}
while(true){
try{
byte[] buf= new byte[100];
receivePacket=new DatagramPacket(buf,buf.length);
receiveSocket.receive(receivePacket);
jtextarea1.append("\n 服务器 :\t");
byte[] data=receivePacket.getData();
String receivedString=new String(data);
jtextarea1.append(receivedString);
}
catch(IOException e){
jtextarea1.append("网络通讯出现错误,问题在" +e.toString());
}
}
}
public static void main(String[] args){
CommunicationClient applet=new CommunicationClient();
applet.isStandalone=true;
Frame frame;
frame=new Frame(){
protected void processWindowEvent(WindowEvent e){
super.processComponentEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
System.exit(0);
}
}
public synchronized void setTitle(String title){
super.setTitle(title);
this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet,BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width)/2,(d.height - frame.getSize().height)/2);
frame.setVisible(true);
}
void jtextfield1_actionPerformed(ActionEvent e){
try{
jtextarea1.append("\n客户端 : ");
String string=jtextfield1.getText();
jtextarea1.append(string);
byte[] databyte=new byte[100];
string.getBytes(0,string.length(),databyte,0);
sendPacket=new DatagramPacket(databyte,string.length(),InetAddress.getByName("192.168.13.47"),8000);
sendSocket.send(sendPacket);
}
catch(IOException e1){
jtextarea1.append("网络通讯出现错误,问题在 "+e1.toString());
}
}
}
java.lang.NullPointerException
at communicationClient.CommunicationClient.waitForPackets(CommunicationClient.java:78)
at communicationClient.CommunicationClient.start(CommunicationClient.java:56)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "thread applet-communicationClient\CommunicationClient.class" java.lang.NullPointerException
at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
at sun.plugin.AppletViewer.showAppletException(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "main" java.lang.NullPointerException
at communicationClient.CommunicationClient.waitForPackets(CommunicationC
lient.java:78)
at communicationClient.CommunicationClient.start(CommunicationClient.jav
a:56)
at communicationClient.CommunicationClient.main(CommunicationClient.java
:111)
Press any key to continue...