Jump to content

Java Help with GUI

super_teabag

Hi again, I've been trying to finish up this java project on break, and I've hit a snag with my GUI.

 

Having trouble trying to get my GUI to display text on my text field. You may need all 4 of my classes in order to get my GUI to properly work

 

I know there's a setText() method in java... Just kinda confused how to use it in this case.

 

I've attached my java files as wordpad docs. If someone can take a look at it It'd be much appreciated. Thanks :)

To my understanding when this a button is pressed, and that buttons name is say east.

I'd call the method "myGame.move("East")" which would move the player east.

 

In the game class there's a method I made called "getMessage()" which gets a local String variable that contains current message.

I would assume the GUI code would then look something like

 

"results.setText(myGame.getMessage())";

results is the text area where I'm displaying the text.

 

but that's giving me errors

*Snipit of my code*

 private class ButtonListener implements ActionListener{        public void actionPerformed(ActionEvent e){                       // extract the button that was clicked            JComponent buttonPressed = (JComponent) e.getSource();            String temp;            if (buttonPressed == quitItem){                System.exit(1);            }else if (buttonPressed == newItem){                newGame();            }else if (buttonPressed == east){                myGame.move("East");                temp = myGame.getMessage();                results.setText(temp);            }else if (buttonPressed == west){                myGame.move("West");                results.setText(myGame.getMessage());            }else if (buttonPressed == north){                myGame.move("North");                results.setText(myGame.getMessage());            }else if (buttonPressed == south){                myGame.move("South");                results.setText(myGame.getMessage());            }else if (buttonPressed == take){                myGame.take();                results.setText(myGame.getMessage());            }else if (buttonPressed == look){                myGame.look();                results.setText(myGame.getMessage());            }else if (buttonPressed == hold){                myGame.show();                results.setText(myGame.getMessage());            }else if (buttonPressed == help){                myGame.help();                results.setText(myGame.getMessage());            }else if (buttonPressed == drop){                String item = JOptionPane.showInputDialog(null, "What do you want to drop?");                if (item != null){                    myGame.drop(item);                  }            }            results.append("\n\n" + myGame.getMessage());               if (myGame.gameOver()){                gameOver();            }        }    }

Item.rtf

Room.rtf

game.rtf

GUI.rtf

Link to comment
Share on other sites

Link to post
Share on other sites

instead of saying it gives me errors, please please please provide details about the errors. follow the stack trace, it will tell you where things go wrong if it can. what errors are you getting?

Link to comment
Share on other sites

Link to post
Share on other sites

Figured it out :). The GUI was provided to me by my professor

Link to comment
Share on other sites

Link to post
Share on other sites

On another note, you shouldn't pass around strings like that, with "east" etc. That's what enums are for

Link to comment
Share on other sites

Link to post
Share on other sites

Figured it out :). The GUI was provided to me by my professor

Please mark as answered then.

I might be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×