--------------------------------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.*;
public class Client extends JFrame implements ActionListener
{
JPanel panelBack, panelTalk;
JTextArea textViewTalk;
JLabel labelTalk, labelTo;
JTextField textTalk, textName;
JButton buttonTalk;
JComboBox listOnline;
GridBagLayout g1;
BorderLayout bd1;
GridBagConstraints gbc;
Socket socket;
BufferedReader in;
PrintWriter out;
String strSend, strReceive, strKey, strStatus;
private StringTokenizer st;
public Client()
{
g1 = new GridBagLayout();
bd1 = new BorderLayout();
gbc = new GridBagConstraints();
panelBack = (JPanel)getContentPane();
panelBack.setLayout(bd1);
buttonTalk = new JButton("Send");
labelTalk = new JLabel("Talk: ");
labelTo = new JLabel("To: ");
textTalk = new JTextField(30);
panelTalk = new JPanel();
textViewTalk = new JTextArea(18,40);
listOnline = new JComboBox();
buttonTalk.addActionListener(this);
listOnline.addItem("All");
panelTalk.add(labelTalk);
panelTalk.add(textTalk);
panelTalk.add(labelTo);
panelTalk.add(listOnline);
panelTalk.add(buttonTalk);
panelBack.add("Center",textViewTalk);
panelBack.add("South",panelTalk);
buttonTalk.setEnabled(false);
setSize(600,450);
show();
}
public static void main(String[] args)
{
new Client();
}
void connectServer()
{
try
{
socket = new Socket("192.168.1.3",8049);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
} catch(Exception e)
{
System.out.println(e);
}
}
private void initLogin() throws IOException
{
strReceive = in.readLine();
st = new StringTokenizer(strReceive,"|");
strStatus = st.nextToken();
if(strStatus.equals("succeed"))
{
buttonTalk.setEnabled(true);
new ClientThread(socket);
out.println("init|online");
}
}
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
try
{
connectServer();
out.println(strSend);
initLogin();
if(obj==buttonTalk)
{
if(textTalk.getText().length()>0)
{
out.println("talk|"+textTalk.getText()+"|"+textName.getText()+"|"+listOnline.getSelectedItem().toString());
textTalk.setText("");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
class ClientThread implements Runnable
{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String strReceive;
private String strKey;
private Thread threadTalk;
private StringTokenizer st;
public ClientThread(Socket s)throws IOException
{
this.socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
threadTalk = new Thread(this);
threadTalk.start();
}
public void run()
{
while(true)
{
synchronized(this)
{
try
{
strReceive = in.readLine();
st = new StringTokenizer(strReceive,"|");
strKey = st.nextToken();
if(strKey.equals("talk"))
{
String strTalk = st.nextToken();
strTalk = textViewTalk.getText()+"\r\n "+strTalk;
textViewTalk.setText(strTalk);
}
else if(strKey.equals("online"))
{
String strOnline;
while(st.hasMoreTokens())
{
strOnline = st.nextToken();
listOnline.addItem(strOnline);
}
}
else if(strKey.equals("remove"))
{
String strRemove;
while(st.hasMoreTokens())
{
strRemove = st.nextToken();
listOnline.removeItem(strRemove);
}
}
Thread.sleep(1000);
}
catch(Exception e){}
}
}
}
}
}
--------------------------------------------------------------------------------
编译时:有错,错误如下:
--------------------Configuration: j2sdk1.5.0 <Default>--------------------
D:\JCreator Pro\MyProjects\Client.java:19: cannot find symbol
symbol : class Socket
location: class Client
Socket socket;
^
D:\JCreator Pro\MyProjects\Client.java:116: cannot find symbol
symbol : class Socket
location: class Client.ClientThread
private Socket socket;
^
D:\JCreator Pro\MyProjects\Client.java:124: cannot find symbol
symbol : class Socket
location: class Client.ClientThread
public ClientThread(Socket s)throws IOException
^
D:\JCreator Pro\MyProjects\Client.java:68: cannot find symbol
symbol : class Socket
location: class Client
socket = new Socket("192.168.1.3",8049);
^
Note: D:\JCreator Pro\MyProjects\Client.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 errorsProcess completed.
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.*;
public class Client extends JFrame implements ActionListener
{
JPanel panelBack, panelTalk;
JTextArea textViewTalk;
JLabel labelTalk, labelTo;
JTextField textTalk, textName;
JButton buttonTalk;
JComboBox listOnline;
GridBagLayout g1;
BorderLayout bd1;
GridBagConstraints gbc;
Socket socket;
BufferedReader in;
PrintWriter out;
String strSend, strReceive, strKey, strStatus;
private StringTokenizer st;
public Client()
{
g1 = new GridBagLayout();
bd1 = new BorderLayout();
gbc = new GridBagConstraints();
panelBack = (JPanel)getContentPane();
panelBack.setLayout(bd1);
buttonTalk = new JButton("Send");
labelTalk = new JLabel("Talk: ");
labelTo = new JLabel("To: ");
textTalk = new JTextField(30);
panelTalk = new JPanel();
textViewTalk = new JTextArea(18,40);
listOnline = new JComboBox();
buttonTalk.addActionListener(this);
listOnline.addItem("All");
panelTalk.add(labelTalk);
panelTalk.add(textTalk);
panelTalk.add(labelTo);
panelTalk.add(listOnline);
panelTalk.add(buttonTalk);
panelBack.add("Center",textViewTalk);
panelBack.add("South",panelTalk);
buttonTalk.setEnabled(false);
setSize(600,450);
show();
}
public static void main(String[] args)
{
new Client();
}
void connectServer()
{
try
{
socket = new Socket("192.168.1.3",8049);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
} catch(Exception e)
{
System.out.println(e);
}
}
private void initLogin() throws IOException
{
strReceive = in.readLine();
st = new StringTokenizer(strReceive,"|");
strStatus = st.nextToken();
if(strStatus.equals("succeed"))
{
buttonTalk.setEnabled(true);
new ClientThread(socket);
out.println("init|online");
}
}
public void actionPerformed(ActionEvent evt)
{
Object obj=evt.getSource();
try
{
connectServer();
out.println(strSend);
initLogin();
if(obj==buttonTalk)
{
if(textTalk.getText().length()>0)
{
out.println("talk|"+textTalk.getText()+"|"+textName.getText()+"|"+listOnline.getSelectedItem().toString());
textTalk.setText("");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
class ClientThread implements Runnable
{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String strReceive;
private String strKey;
private Thread threadTalk;
private StringTokenizer st;
public ClientThread(Socket s)throws IOException
{
this.socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
threadTalk = new Thread(this);
threadTalk.start();
}
public void run()
{
while(true)
{
synchronized(this)
{
try
{
strReceive = in.readLine();
st = new StringTokenizer(strReceive,"|");
strKey = st.nextToken();
if(strKey.equals("talk"))
{
String strTalk = st.nextToken();
strTalk = textViewTalk.getText()+"\r\n "+strTalk;
textViewTalk.setText(strTalk);
}
else if(strKey.equals("online"))
{
String strOnline;
while(st.hasMoreTokens())
{
strOnline = st.nextToken();
listOnline.addItem(strOnline);
}
}
else if(strKey.equals("remove"))
{
String strRemove;
while(st.hasMoreTokens())
{
strRemove = st.nextToken();
listOnline.removeItem(strRemove);
}
}
Thread.sleep(1000);
}
catch(Exception e){}
}
}
}
}
}
--------------------------------------------------------------------------------
编译时:有错,错误如下:
--------------------Configuration: j2sdk1.5.0 <Default>--------------------
D:\JCreator Pro\MyProjects\Client.java:19: cannot find symbol
symbol : class Socket
location: class Client
Socket socket;
^
D:\JCreator Pro\MyProjects\Client.java:116: cannot find symbol
symbol : class Socket
location: class Client.ClientThread
private Socket socket;
^
D:\JCreator Pro\MyProjects\Client.java:124: cannot find symbol
symbol : class Socket
location: class Client.ClientThread
public ClientThread(Socket s)throws IOException
^
D:\JCreator Pro\MyProjects\Client.java:68: cannot find symbol
symbol : class Socket
location: class Client
socket = new Socket("192.168.1.3",8049);
^
Note: D:\JCreator Pro\MyProjects\Client.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 errorsProcess completed.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货