This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am creating a chat application and found a good tutorial online that explains everything.
The code works perfectly but instead of using a JFrame Form to make the GUI for the application they just typed out all the GUI code in a java class file.
I want to use a JFrame GUI so it can be easily changed. I tried making the GUI but I wasn't sure where to put the actionPerformed code
heres the original file
You mean you want something like this?Good idea. Better than making your class extend JFrame.
As far as I am concerned making a display class implement ActionListener is an abomination against object‑oriented programming. Even if you find it in lots of books etc.
Making a GUI class connect to a port is another abomination of design. You should have one class which connects to the port and transmits all the messages. You should be able to run apps using instances of that class without using a GUI at all.etc etc.
You now have code which will send and receive the messages. Do not even attempt to get a GUI working until you have all that running nicely. Make sure your ChatConnector has an API with sendMessage(), receiveReply(), logIn(), logOut() (or similar), etc. methods visible.
Joined: Nov 19, 2012
no that's not what i am trying to do. Most of the code that i showed is automatically generated when created a GUI by dragging and dropping the items eg buttons, onto the form.
so my problem is i don't know where to put some of the code. I created my own GUI and tried putting the code from the first post into it but now its not working.
here is the code. the part that is commented out is the part i need to place into my code. Put i am not sure if i put everything in the right place
Joined: Oct 13, 2005
Karen Wilson wrote:. . . Most of the code that i showed is automatically generated when created a GUI by dragging and dropping the items eg buttons, onto the form.
Yes, you can tell you are using NetBeans from the pages of unintelligible group layout code.
. . . i am not sure if i put everything in the right place . . .
I have already told you you hadn’t. And just to make things worse, you have two lots of field declarations, at each end of the class. As far as I know, NetBeans usually adds individual listeners to your buttons, which is correct, rather than addActionListener(this). You want something like this:-You put the actual logic in the login, logout, sendMessage, etc methods. You write one at a time and try it, with print statements for debugging in the called methods.
Please always use // to comment out code, never /*...*/
Apart from repeating what I said yesterday about different classes which do different things, I don’t think there is a lot more I can say.
Joined: Nov 19, 2012
ok so i am trying one button at a time starting with the the log in button.
my code now looks llike this:
but nothing is happening when i click the button. i don't know what the problem is