Text in GUI

I have made a rock paper scissors game, but have only figured to print the results in output, though it does have a kind of GUI. i was wondering if anyone could show me how to show the printout in the GUI. Here's the code if you wanted to see it:

package components;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class RPSS extends JPanel{

private int compChoice;

private int myChoice;

private String cC;

private String mC;

private int scoreMe;

private int scoreComp;

private JFrame frame;

private JMenuBar menubar;

private JTextArea output;

private JTextArea textArea;

private JScrollPane scrollPane;

public JMenuBar createMenuBar() {

JMenuBar menuBar;

JMenu menu, submenu, options;

JMenuItem menuItem;

JRadioButtonMenuItem rbMenuItem;

JCheckBoxMenuItem cbMenuItem;

menuBar = new JMenuBar();

menu = new JMenu("Choose");

menu.setMnemonic(KeyEvent.VK_A);

menuBar.add(menu);

menuItem = new JMenuItem("Rock",KeyEvent.VK_T);

menuItem.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_1, ActionEvent.CTRL_MASK));

menuItem.getAccessibleContext().setAccessibleDescription("Select rock");

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { meRock(); }

});

menu.add(menuItem);

menuItem = new JMenuItem("Paper");

menuItem.setMnemonic(KeyEvent.VK_B);

menuItem.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_2, ActionEvent.CTRL_MASK));

menuItem.getAccessibleContext().setAccessibleDescription("Select paper");

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { mePaper(); }

});

menu.add(menuItem);

menuItem = new JMenuItem("Scissors");

menuItem.setMnemonic(KeyEvent.VK_D);

menuItem.setAccelerator(KeyStroke.getKeyStroke(

KeyEvent.VK_3, ActionEvent.CTRL_MASK));

menuItem.getAccessibleContext().setAccessibleDescription("Select Scissors");

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { meScissors(); }

});

menu.add(menuItem);

menu.addSeparator();

ButtonGroup group = new ButtonGroup();

menuItem = new JMenuItem("Random");

menuItem.setMnemonic(KeyEvent.VK_R);

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { mCNumber012(); }

});

menu.add(menuItem);

menu.addSeparator();

cbMenuItem = new JCheckBoxMenuItem("Vs computer");

cbMenuItem.setMnemonic(KeyEvent.VK_C);

menu.add(cbMenuItem);

options = new JMenu("Options");

options.setMnemonic(KeyEvent.VK_N);

options.getAccessibleContext().setAccessibleDescription(

"This menu does nothing");

menuBar.add(options);

JMenuItem scores = new JMenuItem("Scores");

scores.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { scoresSheet(); }

});

options.add(scores);

JMenuItem quitItem = new JMenuItem("quit");

quitItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) { quit(); }

});

options.add(quitItem);

return menuBar;

}

public Container createContentPane() {

//Create the content-pane-to-be.

JPanel contentPane = new JPanel(new BorderLayout());

contentPane.setOpaque(true);

//Create a scrolled text area.

output = new JTextArea(5, 30);

output.setEditable(false);

scrollPane = new JScrollPane(output);

//Add the text area to the content pane.

contentPane.add(scrollPane, BorderLayout.CENTER);

return contentPane;

}

private static void createAndShowGUI() {

JFrame frame = new JFrame("MenuLookDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

RPSS demo = new RPSS();

frame.setJMenuBar(demo.createMenuBar());

frame.setContentPane(demo.createContentPane());

frame.setSize(450, 260);

frame.setVisible(true);

}

public static void main(String[] args) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

private void scoresSheet()

{

System.out.println("The score is now " +scoreComp+ " to the computer and " +scoreMe+ " to you");

}

private void randomNumber012()

{

compChoice = (int)(Math.random()*3);

if(compChoice == 0)

{

cC = "Rock";

}

else if(compChoice == 1)

{

cC = "Scissors";

}

else if(compChoice == 2)

{

cC = "Paper";

}

}

private void mCNumber012()

{

myChoice = (int)(Math.random()*3);

if(compChoice == 0)

{

mC = "Rock";

meRock();

}

else if(compChoice == 1)

{

mC = "Scissors";

meScissors();

}

else if(compChoice == 2)

{

mC = "Paper";

mePaper();

}

}

private void compRPS()

{

System.out.println("The computer has chosen "+cC);

}

private void mC()

{

if(myChoice == 0)

{

mC = "Rock";

}

else if(myChoice == 1)

{

mC = "Scissors";

}

else if(myChoice == 2)

{

mC = "Paper";

}

System.out.println("You have chosen: " +mC);

}

private void meRock()

{

myChoice = 0;

mC();

randomNumber012();

compRPS();

Winner();

}

private void mePaper()

{

myChoice = 2;

mC();

randomNumber012();

compRPS();

Winner();

}

private void meScissors()

{

myChoice = 1;

mC();

randomNumber012();

compRPS();

Winner();

}

private void Winner()

{

if(compChoice == -1)

{

System.out.println("computer hasn't chosen yet");

}

else if(compChoice == myChoice)

{

System.out.println("Computer chose: " +cC+ " so it's a draw");

System.out.println("The score is now " +scoreComp+ " to the computer and " +scoreMe+ " to you");

}

else if(compChoice == 0 && myChoice == 1 ||

compChoice == 2 && myChoice == 0 ||

compChoice == 1 && myChoice == 2)

{

System.out.println("Computer chose: " +cC+ " so computer wins");

scoreComp = scoreComp + 1;

System.out.println("The score is now " +scoreComp+ " to the computer and " +scoreMe+ " to you");

}

else

{

System.out.println("Computer chose: " +cC+ " and so you win, well done you");

scoreMe = scoreMe + 1;

System.out.println("The score is now " +scoreComp+ " to the computer and " +scoreMe+ " to you");

}

}

private void quit()

{

System.exit(0);

}

}

[7078 byte] By [mogwairna] at [2007-11-27 8:34:59]
# 1
Now, I didn't read ALL your code (there's a lot) but since you're already using gui and JObjects, you can just toss a Label or JPanel or similar object in and use the setText() method to display results there.
Hybrida at 2007-7-12 20:31:23 > top of Java-index,Java Essentials,New To Java...
# 2
hey sorry to keep asking questions, but i made the label and i cant seem to get setText() to be used to change it, and everytime i try to change teh text using variables, it wont let me due to the variable being non-static in a static method
mogwairna at 2007-7-12 20:31:23 > top of Java-index,Java Essentials,New To Java...
# 3

> hey sorry to keep asking questions, but i made the

> label and i cant seem to get setText() to be used to

> change it, and everytime i try to change teh text

> using variables, it wont let me due to the variable

> being non-static in a static method

1) Please learn to use code tags if you are going to post any more code in the forum.

2) Make your methods non-static. Use OOP techniques. It will help to teach you OOP techniques and will make your code easier to maintain and debug.

petes1234a at 2007-7-12 20:31:23 > top of Java-index,Java Essentials,New To Java...
# 4

> hey sorry to keep asking questions, but i made the

> label and i cant seem to get setText() to be used to

> change it, and everytime i try to change teh text

> using variables, it wont let me due to the variable

> being non-static in a static method

Whatever you put in those System.out.println(); 's put it in the JLabel

x.setText(result);

deAppela at 2007-7-12 20:31:23 > top of Java-index,Java Essentials,New To Java...
# 5

This problem also seems the perfect place to have an enum. this holds a list of constants that represent a rock, paper or scissors value. You can even have a static function in the enum that randomly outputs one of these values. Something like this could be one way to do it:

import java.util.Random;

public enum RPSValue

{

NO_VALUE, // this guy may not be necessary. I have it in out of force of habit.

ROCK,

PAPER,

SCISSORS;

private static Random rand = new Random();

// returns a random ROCK, PAPER, OR SCISSORS

public static RPSValue getRandomValue()

{

int rpsInt = rand.nextInt(3) + 1; // if you get rid of no_value, get rid of the "+ 1"

return RPSValue.values()[rpsInt];

}

// main routine to allow me to test this class

public static void main(String[] args)

{

for (int i = 0; i < 20; i++)

{

System.out.println(RPSValue.getRandomValue());

}

}

}

petes1234a at 2007-7-12 20:31:23 > top of Java-index,Java Essentials,New To Java...