java - Two way communication between a GUI and a Server Client -
i'm trying send string when button pressed in gui. client class running keep taking string commands command line , send them server processed , response returned.
how can send data through gui , move results gui?
e.g. have button called "pickup" which, when clicked send string "pickup" server, through client class.
likewise, response server either "success" or "fail" printed through thread "serverresponse" in client class , needs somehow sent arbitrary method in playergui class parameter.
thanks help, sorry not using conventional class/method/field naming styles!
import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; public class playergui { private jframe frame = new jframe(); private jpanel displaypanel; private jtextpane hosttextpane; private jtextpane porttextpane; private static client newclient; public static void main(string[] args) { playergui gui = new playergui(); gui.frame.setvisible(true); newclient = new client(gui); } /** * create application. */ public playergui() { frame.getcontentpane().setbackground(new color(255, 255, 255)); frame.getcontentpane().setlayout(null); frame.setbounds(100, 100, 500, 630); frame.setundecorated(false); // removes menu bar frame.setdefaultcloseoperation(jframe.exit_on_close); final jpanel humangamewindow = new jpanel(); humangamewindow.setlayout(null); humangamewindow.setbackground(color.light_gray); humangamewindow.setbounds(0, 0, 500, 630); humangamewindow.setvisible(false); jbutton pickup = new jbutton("pickup"); pickup.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { //i want send string "pickup" stars in client.class } }); pickup.setbackground(new color(100, 149, 237)); pickup.setbounds(40, 555, 100, 40); displaypanel = new jpanel(); displaypanel.setbounds(48, 89, 400, 400); displaypanel.setlayout(new gridlayout(5, 5)); displaypanel.setpreferredsize(new dimension((int) (400), (int) (400))); (int = 1; < 26; i++) { displaypanel.add(new jlabel("label " + i)); } jbutton = new jbutton("look"); look.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { } }); look.setbackground(new color(100, 149, 237)); look.setbounds(40, 514, 100, 40); humangamewindow.add(look); humangamewindow.add(pickup); humangamewindow.add(displaypanel); final jpanel mainmenu = new jpanel(); mainmenu.setlayout(null); mainmenu.setbackground(color.dark_gray); mainmenu.setbounds(0, 0, 500, 630); mainmenu.setvisible(true); jlabel mainmenutitle = new jlabel("dungeon of doom!!"); mainmenutitle.setforeground(new color(100, 149, 237)); mainmenutitle.setfont(new font("moire", font.bold, 28)); mainmenutitle.setbounds(50, 13, 380, 50); jbutton mainmenuquit = new jbutton("quit"); mainmenuquit.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { system.exit(0); } }); mainmenuquit.setbackground(new color(100, 149, 237)); mainmenuquit.setbounds(220, 345, 70, 55); jbutton playgamehuman = new jbutton("play game human"); playgamehuman.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { mainmenu.setvisible(false); humangamewindow.setvisible(true); } }); playgamehuman.setbackground(new color(100, 149, 237)); playgamehuman.setbounds(50, 345, 150, 55); mainmenu.add(mainmenutitle); mainmenu.add(mainmenuquit); mainmenu.add(playgamehuman); frame.getcontentpane().add(humangamewindow); frame.getcontentpane().add(mainmenu); frame.setvisible(true); } }
this client class, thread want send response gui class process , display specific output. asterisks want send text button presses in gui class (there other buttons have deleted code easier reading!).
import java.net.*; import java.io.*; public class client{ public client(playergui gui){ try{ final socket sock = new socket("localhost",4444); final datainputstream in = new datainputstream(sock.getinputstream()); final printstream out = new printstream(sock.getoutputstream()); datainputstream inputline = new datainputstream(new bufferedinputstream(system.in)); final thread serverresponse = new thread(){ public void run(){ system.out.println("dungeon of doom has started"); if(sock != null){ if(in != null){ try{ string response; while((response = in.readline()) != null){ //i want send "response" gui class system.out.println(response); } }catch(unknownhostexception uhe){ system.err.println("unknown host1: " + uhe); }catch(ioexception ioe){ system.err.println("ioexception1: " + ioe); }catch(nullpointerexception npe){ system.err.println("null pointer1: " + npe); } } } } }; serverresponse.start(); if(sock != null){ if(out != null){ try{ while(true){ string sending = ************************* //string sending = inputline.readline(); out.println(sending); if(sending.equals("quit")) break; } }catch(unknownhostexception uhe2){ system.err.println("unknown host2: " + uhe2); }catch(ioexception ioe2){ system.err.println("ioexception2: " + ioe2); }catch(nullpointerexception npe2){ system.err.println("null pointer2: " + npe2); } } } out.close(); in.close(); sock.close(); }catch(unknownhostexception uhe3){ system.err.println("unknown host3: " + uhe3); }catch(ioexception ioe3){ system.err.println("ioexception3: " + ioe3); }catch(nullpointerexception npe3){ system.err.println("null pointer3: " + npe3); } } }
you probaly should read , ask questions: tutorial
Comments
Post a Comment