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);
}
}

