The client contains 10 files, corresponding to 9 forms and a custom friendsinfo class, used to save information about friends and strangers obtained from the server. the compiling process of the client is the process of learning how to use Java swing, so the code is a basic update of the swing component settings. below is my implementation:
Implement fmlogin. Java on the logon Interface
Package client;
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Import java.net .*;
Import java. Io .*;
Import com. Borland. jbcl. layout .*;
Import javax. Swing. Border .*;
Import java. util. vector;
Public class fmlogin extends jframe {
Xylayout xylayout1 = new xylayout ();
Jpanel jpanel1 = new jpanel ();
Jlabel jlabel2 = new jlabel ();
Jlabel jlabel1 = new jlabel ();
Xylayout xylayout2 = new xylayout ();
Jtextfield jtextfield1 = new jtextfield ();
Jpasswordfield jpasswordfield1 = new jpasswordfield ();
Jbutton jbutton1 = new jbutton ();
Jbutton jbutton2 = new jbutton ();
Jbutton jbutton3 = new jbutton ();
Jlabel jlabel3 = new jlabel ();
Jlabel jlabel4 = new jlabel ();
Jtextfield servername = new jtextfield ();
//*****************
String server; // server name
Static int serport = 10000; // Port
Private Socket socket;
Private bufferedreader in; // input/output stream
Private printwriter out;
Jlabel jlabel5 = new jlabel ();
Public fmlogin (){
Try {
Jbinit ();
}
Catch (exception ex ){
Ex. printstacktrace ();
}
}
Void jbinit () throws exception {
This. getcontentpane (). setbackground (systemcolor. desktop );
This. setforeground (color. White );
This. setresizable (false );
This. setstate (frame. Normal );
This. settitle ("jicq Logon ");
Xylayout1.setwidth (267 );
Xylayout1.setheight (189 );
This. getcontentpane (). setlayout (xylayout1 );
Jlabel2.setfont (New java. AWT. Font ("", 0, 15 ));
Jlabel2.setforeground (systemcolor. desktop );
Jlabel2.setborder (null );
Jlabel2.setopaque (false );
Jlabel2.setrequestfocusenabled (true );
Jlabel2.settooltiptext ("");
Jlabel2.settext ("Password :");
Jlabel1.setfont (New java. AWT. Font ("", 0, 15 ));
Jlabel1.setforeground (systemcolor. desktop );
Jlabel1.setborder (null );
Jlabel1.settext ("jicq No :");
Jpanel1.setlayout (xylayout2 );
Jpanel1.setbackground (systemcolor. inactivecaptiontext );
Jpanel1.setforeground (color. Black );
Jpanel1.setborder (borderfactory. createloweredbevelborder ());
Jtextfield1.setbackground (systemcolor. Text );
Jtextfield1.setforeground (uimanager. getcolor ("texttext "));
Jtextfield1.settext ("");
Jpasswordfield1.setbackground (systemcolor. Text );
Jpasswordfield1.setforeground (uimanager. getcolor ("texttext "));
Jpasswordfield1.settext ("");
Jbutton1.setbackground (systemcolor. activecaptiontext );
Jbutton1.setfont (New java. AWT. Font ("", 0, 14 ));
Jbutton1.setforeground (systemcolor. desktop );
Jbutton1.setborder (borderfactory. createlineborder (color. Black ));
Jbutton1.setborderpainted (true );
Jbutton1.setmargin (New insets (2, 14, 2, 14 ));
Jbutton1.settext ("login ");
Jbutton1.addactionlistener (New fmlogin_jbutton?actionadapter (this ));
Jbutton2.setbackground (systemcolor. activecaptiontext );
Jbutton2.setfont (New java. AWT. Font ("", 0, 14 ));
Jbutton2.setforeground (systemcolor. desktop );
Jbutton2.setborder (borderfactory. createlineborder (color. Black ));
Jbutton2.setborderpainted (true );
Jbutton2.setmargin (New insets (2, 14, 2, 14 ));
Jbutton2.settext (" ");
Jbutton2.addactionlistener (New fmlogin_jbutton2_actionadapter (this ));
Jbutton3.setbackground (systemcolor. activecaptiontext );
Jbutton3.setfont (New java. AWT. Font ("", 0, 14 ));
Jbutton3.setforeground (systemcolor. desktop );
Jbutton3.setborder (borderfactory. createlineborder (color. Black ));
Jbutton3.setborderpainted (true );
Jbutton3.setmargin (New insets (2, 14, 2, 14 ));
Jbutton3.settext ("Remove ");
Jbutton3.addactionlistener (New fmlogin_jbutton3_actionadapter (this ));
Jlabel3.setbackground (color. White );
Jlabel3.setfont (New java. AWT. Font ("Ms reference sans serif", 1, 18 ));
Jlabel3.setforeground (systemcolor. inactivecaptiontext );
Jlabel3.setborder (null );
Jlabel3.sethorizontalalignment (swingconstants. center );
Jlabel3.settext ("jicq 2005 beta ");
Jlabel4.settext ("server/IP :");
Jlabel4.settooltiptext ("");
Jlabel4.setrequestfocusenabled (true );
Jlabel4.setopaque (false );
Jlabel4.setborder (null );
Jlabel4.setforeground (systemcolor. desktop );
Jlabel4.setfont (New java. AWT. Font ("", 0, 15 ));
Servername. settext ("localhost ");
Servername. setbackground (systemcolor. Text );
Servername. setforeground (uimanager. getcolor ("texttext "));
Jlabel5.setforeground (systemcolor. inactivecaptiontext );
Jlabel5.sethorizontalalignment (swingconstants. Right );
Jlabel5.settext ("[copyright] jeven2005 ");
Jpanel1.add (jtextfield1, new xyconstraints (98, 8,138, 19 ));
Jpanel1.add (jlabel1, new xyconstraints (16, 8, 85,-1 ));
Jpanel1.add (jlabel2, new xyconstraints (20, 36, 77,-1 ));
Jpanel1.add (jpasswordfield1, new xyconstraints (97, 37,138, 19 ));
Jpanel1.add (jlabel4, new xyconstraints (13, 65, 92,-1 ));
Jpanel1.add (servername, new xyconstraints (97, 65,138, 19 ));
Jpanel1.add (jbutton1, new xyconstraints (8, 93, 78, 22 ));
Jpanel1.add (jbutton3, new xyconstraints (163, 93, 81, 22 ));
Jpanel1.add (jbutton2, new xyconstraints (85, 93, 79, 22 ));
This. getcontentpane (). Add (jlabel5, new xyconstraints (106,167,150, 16 ));
This. getcontentpane (). Add (jlabel3, new xyconstraints (3, 2,262, 26 ));
This. getcontentpane (). Add (jpanel1, new xyconstraints (5, 32,256,127 ));
This. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
}
Void jbutton?action=med (actionevent e ){
Try {
Vector myinfo = new vector ();
String myjicq = This. jtextfield1.gettext (). Trim ();
Server = This. servername. gettext ();
// Serport = integer. parseint (this. SERVERPORT. gettext ());
System. Out. println ("Login button clicked ");
Socket socket = new socket (inetaddress. getbyname (server), serport); // connect to the server
System. Out. println ("connected to server ");
Bufferedreader in = new bufferedreader (New inputstreamreader (socket. getinputstream ()));
Printwriter out = new printwriter (New bufferedwriter (
New outputstreamwriter (socket. getoutputstream (), true );
Out. println ("login"); // tell the server that I want to log on
Out. println (this. jtextfield1.gettext (). Trim ());
Out. println (this. jpasswordfield1.getpassword ());
System. Out. println ("the PWD:" + jpasswordfield1.getpassword ());
String STR = "";
// Do {
STR = in. Readline (). Trim (); // read the message from the server
// If it fails, an error is reported.
If (Str. Equals ("pwderror") joptionpane. showmessagedialog (this, "the password is invalid. Please re-enter the password:-(", "jicq2005 prompt", joptionpane. information_message );
Else if (Str. equals ("usererror") joptionpane. showmessagedialog (this, "Your jicq number is incorrect. Please enter the correct jicq number", "jicq2005 prompt", joptionpane. information_message );
Else if (Str. Equals ("false") joptionpane. showmessagedialog (this, "Sorry, the server is busy. Please try again later! "," Jicq2005 prompt ", joptionpane. information_message );
Else {// if it succeeds, it first receives its personal information and then opens the main program
String name, IP, status, headpic, email, info, comefrom, sex;
Name = in. Readline ();
IP = in. Readline ();
Status = in. Readline ();
Headpic = in. Readline ();
Email = in. Readline ();
Info = in. Readline ();
Comefrom = in. Readline ();
Sex = in. Readline ();
Myinfo. Add (name );
Myinfo. Add (myjicq );
Myinfo. Add (IP );
Myinfo. Add (Status );
Myinfo. Add (headpic );
Myinfo. Add (email );
Myinfo. Add (Info );
Myinfo. Add (comefrom );
Myinfo. Add (sex );
System. Out. println ("received myinfo successfully :");
// System. Out. println ("myinfo. Size:" + myinfo. Size ());
This. Dispose ();
// Int G = integer. parseint (this. jtextfield1.gettext ());
Fmmain main = new fmmain (myinfo, server, serport); // changed from: fmmain (G, servername, servport );
Main. Pack ();
Dimension screensize = toolkit. getdefatooltoolkit (). getscreensize ();
// Slide the form from the top right corner of the screen
Double left = screensize. getwidth ()-Main. getsize (). getwidth ();
Main. setlocation (INT) left, 0 );
Main. setvisible (true );
For (INT h = 0; H <= 445; H + = 1 ){
Main. setsize (183, H );
}
Main. setresizable (false );
// Main. setlocation ();
}
// System. Out. println ("/N ");
//} While (! Str. Equals ("OK "));
} Catch (ioexception E1) {system. Out. println (e1.getmessage ());}
}
Void jbutton2_actionreceivmed (actionevent e ){
Server = servername. gettext (). tostring (). Trim ();
This. Dispose ();
Fmregister d = new fmregister (server, serport); // open the new window
D. Pack ();
D. setlocationrelativeto (this );
D. Show ();
}
Void jbutton3_actionreceivmed (actionevent e ){
This. Dispose ();
System. Exit (0 );
}
Public static void main (string [] ARGs ){
Fmlogin login = new fmlogin ();
Login. Pack ();
// Center the window
Dimension screensize = toolkit. getdefatooltoolkit (). getscreensize ();
Dimension framesize = login. getsize ();
If (framesize. Height> screensize. Height ){
Framesize. Height = screensize. height;
}
If (framesize. width> screensize. width ){
Framesize. width = screensize. width;
}
Login. setlocation (screensize. Width-framesize. width)/2, (screensize. Height-framesize. Height)/2 );
Login. setvisible (true );
}
}
Class fmlogin_jbutton?actionadapter implements java. AWT. event. actionlistener {
Fmlogin adaptee;
Fmlogin_jbutton?actionadapter (fmlogin adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton?action=med (E );
}
}
Class fmlogin_jbutton2_actionadapter implements java. AWT. event. actionlistener {
Fmlogin adaptee;
Fmlogin_jbutton2_actionadapter (fmlogin adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton2_actionreceivmed (E );
}
}
Class fmlogin_jbutton3_actionadapter implements java. AWT. event. actionlistener {
Fmlogin adaptee;
Fmlogin_jbutton3_actionadapter (fmlogin adaptee ){
This. adaptee = adaptee;
}
Public void actionreceivmed (actionevent e ){
Adaptee. jbutton3_actionreceivmed (E );
}
}