简体   繁体   English

如何在我的游戏中实现ActionListeners?

[英]How to implement ActionListeners to my game?

I am a 15 year old new to the stackoverflow community. 我是Stackoverflow社区的15岁新手。 I am currently trying to independently develop a Rock Paper Scissors game using java. 我目前正在尝试使用Java独立开发Rock Paper Scissors游戏。 I thought working on this project would be an informing experience to help me learn java and its fundamentals. 我认为从事该项目将是一个有益的经验,可以帮助我学习Java及其基础知识。 I am somewhat new with the java programming language so please do not criticize me, I am slowly learning by trial and error. 我对Java编程语言有些陌生,所以请不要批评我,我正在反复试验中慢慢学习。 For this particular project, I decided to use Eclipse as I like its user interface better than other IDEs. 对于这个特定项目,我决定使用Eclipse,因为我比其他IDE更喜欢它的用户界面。 Anyway, I decided to implement JPanel to make the game more visual. 无论如何,我决定实现JPanel以使游戏更加可视化。 My code is as follows copy and pasted from my eclipse project: 我的代码如下,并从我的Eclipse项目复制并粘贴:

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. 但是,我正在寻找一种添加方法,当在JPanel上单击石头,纸张或剪刀按钮时,它会注册该单击。 I know this can be acheived with either a Mouse or ActionListener but I could not figure it out on my own. 我知道可以使用Mouse或ActionListener来实现,但我自己无法解决。 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. 在这种情况下,您实现了一个interface ActionListener接口。 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 基本上, interface是一种契约性协议,意味着对实现interface类将满足interface的要求,在这种情况下,这意味着提供由ActionListener接口声明的actionPerformed方法的实现。

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 使用JFC / Swing创建GUI如何使用按钮,复选框和单选按钮以及如何编写动作侦听器

Why exactly is your class abstract? 为什么您的课程到底是抽象的? I assume you are forgetting to implement something, and making it abstract doesn't necessarily fix that. 我认为您忘记了实现某些东西,而使其抽象化并不一定能解决该问题。

to add a ActionListener to a JButton ...this is what i usually do...: 将动作侦听器添加到JButton ...这通常是我做的...:

public class ButtonHandler implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {

    if(event.getActionCommand().equals("0"))//.getActionCommand() returns the text in the component
        {
            //do what you want here...
        }
            }
}

now to make it function just 现在使它起作用

btn0.addActionListener(new ButtonHandler());//ButtonHandler is the name of the class that implements the ActionListener, btn0 is the name of the JButton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM