Note that there are some explanatory texts on larger screens.

plurals
  1. POJava GUI: Rock, scissor paper game
    text
    copied!<p>I am currently learning about GUI development in Java, and I am supposed to make a rock, paper, scissor game. So far I've made the GUI itself (an ugly one, but still a GUI), but I don't know how to "connect" the selection you make into if's and else's. This is what I have so far:</p> <pre><code>import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Oppgave extends JFrame implements ActionListener{ public JLabel title; public JButton button; public JList liste; public JList liste2; public Oppgave(){ super("A game"); setLayout(new BorderLayout()); title = new JLabel("Rock, scissor, paper!"); add(title, BorderLayout.NORTH); title.setHorizontalAlignment(SwingConstants.CENTER); String[] choice = {"Rock","scissor","paper"}; liste = new JList(choice); liste.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); add(liste, BorderLayout.WEST); liste2 = new JList(choice); liste2.setSelectionMode(DefaultListSelectionModel.SINGLE_SELECTION); add(liste2, BorderLayout.EAST); button = new JButton("Play"); add(button, BorderLayout.SOUTH); button.addActionListener(this); } public void actionPerformed(ActionEvent e){ if(e.getSource().equals(button)){ JOptionPane.showMessageDialog(null, "Player 1 chose: "+liste.getSelectedValue()); JOptionPane.showMessageDialog(null, "Player 2 chose: "+liste2.getSelectedValue()); } } } </code></pre> <p>So now I want to make if's and else's, like if player 1 picks rock, then check what player 2 picks and display the winner. </p> <p>How do I use the selections from the JLists in if/else statements? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload