当前位置: 代码迷 >> Java相关 >> [求助]一个两人聊天室的代码,有错误找不出来!
  详细解决方案

[求助]一个两人聊天室的代码,有错误找不出来!

热度:252   发布时间:2006-04-01 16:00:00.0
[求助]一个两人聊天室的代码,有错误找不出来!

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class ChatFrame extends JFrame
{
JPanel contentPane;
Label label1 = new Label(); //发送消息
Label label2 = new Label(); //输入IP
Label label3 = new Label(); //输入端口标签
Label label4 = new Label(); //输出端口标签
TextField textField1 = new TextField(); //输入IP
TextField textField2 = new TextField(); //发送消息
TextField textField3 = new TextField(); //获得输入端口
TextField textField4 = new TextField(); //获得输出端口
Button button1 = new Button(); //发送消息
Button button2 = new Button(); //建立连接
TextArea textArea1 = new TextArea(); //显示信息

boolean online = flase; //连接标志
int portIn = 5000; //输入端口
int portOut = 5001; //输出端口

DatagramPacket sendpacket,receivepacket; //声明发送和接收数据包
DatagramSocket sendsocket,receivesocket; //声明发送和接受数据接口

public ChatFrame(){}
private void toInit() throws Exception {} //初始化窗体
protected void processWindowEvent(WindowEvent e) {} //关闭窗口
public void waitforpackets() {} //接受消息
void button1_actionPerformed(ActionEvent e) {} //发送消息
void buuton2_actionPerformed(ActionEvent e) {} //建立连接
}

private void toInit() throws Exception
{
contentPane = (JPanel)this.getContentPane();
label1.setText("发送消息");
label1.setBounds(new Rectangle(13, 100, 60, 21));
contentPane.setLayout(null);
this.setSize(new Dimension(363, 320));
this.setTitle("二人聊天室");
label2.setText("输入对方IP地址:");
label2.setBounds(new Rectangle(12, 19, 109, 23));
label3.setText("输入端口:");
label3.setBounds(new Rectangle(12, 58, 55, 22));
label4.setText("输出端口:");
label4.setBounds(new Rectangle(180, 58, 55, 22));
textField3.setText("5000");
textField4.setText("5001");
textField2.setText("0.0.0.0");

button1.setLabel("发送");
button1.setBounds(new Rectangle(280, 100, 59, 21));
button1.addActionListener(new java.awt.event.ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});

button2.setLabel("连接");
button2.setBounds(new Rectangle(280, 100, 59, 21));
button2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
public void actionPerformed(ActionEvent e)
{
button2_actionPerformed(e);
}
}
});

textField4.setBounds(new Rectangle(240, 58, 90, 22));
textField3.setBounds(new Rectangle(70, 58, 90, 22));
textField2.setBounds(new Rectangle(123, 20, 120, 19));
textField1.setBounds(new Rectangle(76, 100, 190, 19));
textField1.setEditable(flase);

contentPane.add(label4, null);
contentPane.add(label3, null);
contentPane.add(label2, null);
contentPane.add(label1, null);
contentPane.add(textField1, null);
contentPane.add(textField2, null);
contentPane.add(textField3, null);
contentPane.add(textField4, null);
contentPane.add(textArea1, null);
contentPane.add(button1, null);
contentPane.add(button2, null);

try
{
sendsocket = new DatagramSocket(5001); //作为输出端
receivesocket = new DatagramSocket(5000); //作为输入端

}
catch(SocketException se)
{
se.printStackTrace();
System.exit(0);
}
}

void button2_actionPerformed(ActionEvent e)
{
String str2 = new String();
String Str3 = new String();
str2 = textField3.getText();
str3 = textField4.getText();

if(str2.compareTo("")!=0 && str3.compareTo("")!=0)
{
portIn = (int) Integer.parseInt(str2);
portOut = (int) Integer.parseInt(str3);

if(online)
{
sendsocket.close();
receivesocket.close();
textArea1.append("\n formal connection closed");
}
try
{
textArea1.append("\n building a connection use the port defined");
sendcocket = new DatagramSocket(portOut);
receivesocket = new DatagramSocket(portIn);
textArea1.append("\n connection builded.");
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(0);
}
online = sendsocket.isClosed();
}
else
{
textArea1.append("\n please input port and ip");
}
}

public void waitforpackets() //检测接受对方发来的信息
{
while(true)
{
try
{
byte[] Array = new byte[100];
receivepacket = new DatagramPacket(Array,Array.length);
receivesocket.receive(receivepacket);
textArea1.append("\nfrom" + receivepacket.getAddress() + ":" + recivepacket.getPort() + " get:");
byte data[] = receivepacket.getData();
String received = new String (data,0);
textArea1.append(received);
}
catch(IOException se)
{
se.printStackTrace();
}
}
}

void button1_actionPerformed(ActionEvent e)
{
String str = textField2.getText();

if(online)
{
if(str.compareTo("")!=0)
{
try
{
textArea1.append("\nto " + textField2.getText() + ":" + portOut + " say: " + textField1.getText());
String s = TextField1.getText();
byte data[] = new byte[100];
s.getBytes(0, s.leng(), data, 0);
sendpacket = new DatagramPacket(data, s.length(), InetAddress.getByName(textField2.getText()), portOut);
sendsocket.send(sendpacket);

}
catch(IOException exc)
{
exc.printStackTrace();
}
}
}
else
{
textArea1.append("\nplease build a connection first!");
}
}

//第38行有错误,说啥也找不出来了,请大家帮忙!

搜索更多相关的解决方案: 聊天室  代码  

----------------解决方案--------------------------------------------------------

一看C形式的代码,没有必要先声明函数再实现的,这是JAVA,不是C
你的程序何止38行有错误,到处都有错误

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;//这个根本不用导入的

public class ChatFrame extends JFrame
{
JPanel contentPane;
Label label1 = new Label(); //发送消息
Label label2 = new Label(); //输入IP
Label label3 = new Label(); //输入端口标签
Label label4 = new Label(); //输出端口标签
TextField textField1 = new TextField(); //输入IP
TextField textField2 = new TextField(); //发送消息
TextField textField3 = new TextField(); //获得输入端口
TextField textField4 = new TextField(); //获得输出端口
Button button1 = new Button(); //发送消息
Button button2 = new Button(); //建立连接
TextArea textArea1 = new TextArea(); //显示信息

boolean online = flase; //连接标志
int portIn = 5000; //输入端口
int portOut = 5001; //输出端口

DatagramPacket sendpacket,receivepacket; //声明发送和接收数据包
DatagramSocket sendsocket,receivesocket; //声明发送和接受数据接口

public ChatFrame(){}
private void toInit() throws Exception {} //初始化窗体
protected void processWindowEvent(WindowEvent e) {} //关闭窗口
public void waitforpackets() {} //接受消息
void button1_actionPerformed(ActionEvent e) {} //发送消息
void buuton2_actionPerformed(ActionEvent e) {} //建立连接
//你的声明是多余的,
}
//你的大括号在这里断了,配不到对了,
private void toInit() throws Exception
{
contentPane = (JPanel)this.getContentPane();
label1.setText("发送消息");
label1.setBounds(new Rectangle(13, 100, 60, 21));
contentPane.setLayout(null);
this.setSize(new Dimension(363, 320));
this.setTitle("二人聊天室");
label2.setText("输入对方IP地址:");
label2.setBounds(new Rectangle(12, 19, 109, 23));
label3.setText("输入端口:");
label3.setBounds(new Rectangle(12, 58, 55, 22));
label4.setText("输出端口:");
label4.setBounds(new Rectangle(180, 58, 55, 22));
textField3.setText("5000");
textField4.setText("5001");
textField2.setText("0.0.0.0");

button1.setLabel("发送");
button1.setBounds(new Rectangle(280, 100, 59, 21));
button1.addActionListener(new java.awt.event.ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
button1_actionPerformed(e);
}
});

button2.setLabel("连接");
button2.setBounds(new Rectangle(280, 100, 59, 21));
button2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
public void actionPerformed(ActionEvent e)//怎么会有两个一样的方法
{
button2_actionPerformed(e);
}
}
});

textField4.setBounds(new Rectangle(240, 58, 90, 22));
textField3.setBounds(new Rectangle(70, 58, 90, 22));
textField2.setBounds(new Rectangle(123, 20, 120, 19));
textField1.setBounds(new Rectangle(76, 100, 190, 19));
textField1.setEditable(flase);//应该是false

contentPane.add(label4, null);
contentPane.add(label3, null);
contentPane.add(label2, null);
contentPane.add(label1, null);
contentPane.add(textField1, null);
contentPane.add(textField2, null);
contentPane.add(textField3, null);
contentPane.add(textField4, null);
contentPane.add(textArea1, null);
contentPane.add(button1, null);
contentPane.add(button2, null);

try
{
sendsocket = new DatagramSocket(5001); //作为输出端
receivesocket = new DatagramSocket(5000); //作为输入端

}
catch(SocketException se)
{
se.printStackTrace();
System.exit(0);
}
}

void button2_actionPerformed(ActionEvent e)
{
String str2 = new String();
String Str3 = new String();//你在这里S是大写的,为什么后面的都是小写了
str2 = textField3.getText();
str3 = textField4.getText();//后面还有好多

if(str2.compareTo("")!=0 && str3.compareTo("")!=0)
{
portIn = (int) Integer.parseInt(str2);
portOut = (int) Integer.parseInt(str3);

if(online)
{
sendsocket.close();
receivesocket.close();
textArea1.append("\n formal connection closed");
}
try
{
textArea1.append("\n building a connection use the port defined");
sendcocket = new DatagramSocket(portOut);
receivesocket = new DatagramSocket(portIn);
textArea1.append("\n connection builded.");
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(0);
}
online = sendsocket.isClosed();
}
else
{
textArea1.append("\n please input port and ip");
}
}

public void waitforpackets() //检测接受对方发来的信息
{
while(true)
{
try
{
byte[] Array = new byte[100];
receivepacket = new DatagramPacket(Array,Array.length);
receivesocket.receive(receivepacket);
textArea1.append("\nfrom" + receivepacket.getAddress() + ":" + recivepacket.getPort() + " get:");
byte data[] = receivepacket.getData();
String received = new String (data,0);
textArea1.append(received);
}
catch(IOException se)
{
se.printStackTrace();
}
}
}

void button1_actionPerformed(ActionEvent e)
{
String str = textField2.getText();

if(online)
{
if(str.compareTo("")!=0)
{
try
{
textArea1.append("\nto " + textField2.getText() + ":" + portOut + " say: " + textField1.getText());
String s = TextField1.getText();
byte data[] = new byte[100];
s.getBytes(0, s.leng(), data, 0);//应该是length();
sendpacket = new DatagramPacket(data, s.length(), InetAddress.getByName(textField2.getText()), portOut);
sendsocket.send(sendpacket);

}
catch(IOException exc)
{
exc.printStackTrace();
}
}
}
else
{
textArea1.append("\nplease build a connection first!");
}
}
应该还有错误,自己慢慢检查吧
如果你是初学 者,那做这种程序真的是不应该,你应该从基础做起
如果你不是初学者,那做这种程序更不应该,  你应该了解了基础的语法
总之,要努力,要仔细


----------------解决方案--------------------------------------------------------
哪个大括号断了?为什么我还找不到呢?
----------------解决方案--------------------------------------------------------
就在我那句话之前,我那句话之前的那个大括号就已经把
public class ChatFrame extends JFrame
{
给对上了,所以后面写的任何东西都不在这个类里面
所以private void toInit() throws Exception
你这句才会出错,系统才会要你加上class或interface的标志
因为不可能在一个类的外面定义一个方法,方法必须在类的里面

----------------解决方案--------------------------------------------------------
所有错误都改完了。。。。。。

这次的代码量有点大,我有些顾及不过来,主要还是想挑战一下自己。

不过还有个问题在里面。

说有ChatFrame覆盖了过时的API,覆盖了哪个呢?
----------------解决方案--------------------------------------------------------
button1.setLabel("发送");
setLabel()这个方法已经过时了,被替换为setText();
----------------解决方案--------------------------------------------------------
谢了
----------------解决方案--------------------------------------------------------
  相关解决方案