Jump to content

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
https://linustechtips.com/topic/258785-java-help-with-gui/
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

×