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();
Label label3=new Label();
Label label4=new Label();
TextField textfield1=new TextField();
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=false;
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 button2_actionPerformed(ActionEvent e) {}
}
private void toInit() throws Exception { //这是报错的地方
contentPane=(JPanel)this.getContentPane();
label1.setText("send Messages:");
label1.setBounds(new Rectangle(13,100,60,21));
contentPane.setLayout(null);
this.setSize(new Dimension(363,320));
this.setTitle("Chat Room Designed For Two People");
label2.setText("Enter the Chater's ip:");
label2.setBounds(new Rectangle(12,19,109,23));
label3.setText("Enter the portIn:");
label3.setBounds(new Rectangle(12,58,55,22));
label4.setText("Enter the portOut:");
label4.setBounds(new Rectangle(180,58,55,22));
textField3.setText("5000");
textField4.setText("5001");
textField2.setText("222.16.47.164");
button1.setLabel("send");
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("connection");
button2.setBounds(new Rectangle(280,20,59,21));
button2.addActionListener(new java.awt.event.ActionLisener() {
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));
textArea1.setBounds(new Rectangle(11,140,333,134));
textArea1.setEditable(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();
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...");
sendsocket=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("\nplease innput ports 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"+reveivepacket.getAddress()
+":"+receivepacket.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.length(),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!");
}
这是报错信息
--------------------Configuration: JDK version 1.4 <Default>--------------------
D:\JCreator Pro\MyProjects\ChatRoom\ChatFrame.java:37: 'class' or 'interface' expected
private void toInit() throws Exception {
^
1 error
Process completed.
该怎么改正呢?是什么原因造成这个错误的?
求达人指点
非常感激
[此贴子已经被作者于2006-5-3 16:23:42编辑过]