I am a 15 year old new to the stackoverflow community. I am currently trying to independently develop a Rock Paper Scissors game using java. I thought working on this project would be an informing experience to help me learn java and its fundamentals. I am somewhat new with the java programming language so please do not criticize me, I am slowly learning by trial and error. For this particular project, I decided to use Eclipse as I like its user interface better than other IDEs. Anyway, I decided to implement JPanel to make the game more visual. My code is as follows copy and pasted from my eclipse project:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public abstract class prompt extends JPanel implements ActionListener {
public static void main(String []args) {
JPanel panel = new JPanel();
JButton rockButton = new JButton("ROCK");
JButton scissorsButton = new JButton("SCISSORS");
JButton paperButton = new JButton("PAPER");
JFrame choicePrompt = new JFrame("Rock, Paper, Scissors Game");
choicePrompt.add(panel);
choicePrompt.setSize(300, 300);
choicePrompt.setVisible(true);
panel.add(rockButton);
panel.add(scissorsButton);
panel.add(paperButton);
}
}
PLEASE READ: I made the prompt class an abstract class because apparently it fixed an error. However, what I'm looking for is a way to add that when the rock, paper, or scissors button is clicked on the JPanel, that it registers that click. I know this can be acheived with either a Mouse or ActionListener but I could not figure it out on my own. All the other "shtuff" like the computerchoice I can potentially do on my own.
PLEASE READ: I made the prompt class an abstract class because apparently it fixed an error
It might have fixed the compiler error, but it won't have fixed the problem.
You class implements an interface
in this case the ActionListener
interface. Basically, an interface
is a contractual agreement that means that classes the implement the interface
will fulfil the requirements of the interface, in this case, this means providing an implementation of the actionPerformed
method declared by the ActionListener
interface
Start by having a read through What Is an Interface?, Creating a GUI With JFC/Swing, How to Use Buttons, Check Boxes, and Radio Buttons and How to Write an Action Listener