繁体   English   中英

在Java GUI中创建计数器

[英]Creating a counter in a Java GUI

我已经在Java GUI中制作了Rock Paper Scissors游戏。 这非常简单,现在我需要帮助的唯一最后一块就是在GUI中创建计数器或记分板。 我知道SO上还有一些其他链接,但是似乎没有一个能够帮助我解决我的特定问题。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class rpsGUItest extends JFrame{

private static final long serialVersionUID = 1L;

public rpsGUItest(){
    super("Rock, Paper, Scissors");


    //settings of the GUI
    setSize(600, 400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    //Creating the panels
    JPanel head = new JPanel();
    JPanel body = new JPanel(new GridBagLayout());
    JPanel footer = new JPanel();


    //Creating the Buttons
    JButton rock = new JButton("Rock");
    JButton paper = new JButton("Paper");
    JButton scissors = new JButton("Scissors");
    JLabel label = new JLabel("Label");

    //Button Functions
    rock.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            double picker = Math.floor(Math.random()*3);
            if(picker == 0.0){
                JOptionPane.showMessageDialog(null, "You picked Rock, the Computer picked Rock, it's a tie!");


            }
            else if(picker == 1.0){
                JOptionPane.showMessageDialog(null, "You picked Rock, the Computer picked Paper, the Computer Wins!, for some reason.");


            }
            else if(picker == 2.0){
            JOptionPane.showMessageDialog(null, "You picked Rock, the Computer picked Scissors, You win! ");

            } 
        }
    });
    paper.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            double picker = Math.floor(Math.random()*3);
            if(picker == 0.0){
                JOptionPane.showMessageDialog(null, "You picked Paper, the Computer picked Rock, You win!");


            }
            else if(picker == 1.0){
                JOptionPane.showMessageDialog(null, "You picked Paper, the Computer picked Paper, it's a tie!");


            }
            else if(picker == 2.0){
            JOptionPane.showMessageDialog(null, "You picked Paper, the Computer picked Scissors, the Computer wins! ");
            }



        }
    });
    scissors.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            double picker = Math.floor(Math.random()*3);
            if(picker == 0.0){
                JOptionPane.showMessageDialog(null, "You picked Scissors, the Computer picked Rock, the Computer wins!");


            }
            else if(picker == 1.0){
                JOptionPane.showMessageDialog(null, "You picked Scissors, the Computer picked Paper, You win!");


            }
            else if(picker == 2.0){
            JOptionPane.showMessageDialog(null, "You picked Scissors, the Computer picked Scissors, it's a tie!");
            }


        }
    });
    //Head Panel
    JLabel title = new JLabel("ROCK PAPER SCISSORS");
    head.add(title);

    //Creating the Check Boxes


    //add the Check Boxes to Panel 2


    //Creating the label and text area


    //GridBag Spacing Stuff
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(15,15,15,15);



    //Adding things to the body
    gbc.gridx = 2;
    gbc.gridy = 0;
    body.add(label);

    gbc.gridx = 0;
    gbc.gridy = 1;
    body.add(rock, gbc);
    gbc.gridx = 0;
    gbc.gridy = 2;
    body.add(paper, gbc);
    gbc.gridx = 0;
    gbc.gridy = 3;
    body.add(scissors, gbc);
    //positioning the panel's 
    add(footer, BorderLayout.SOUTH);
    add(body, BorderLayout.CENTER);
    add(head, BorderLayout.NORTH);
}

我知道这是很多代码,但是我不确定需要哪些部分。 抱歉! 任何帮助将不胜感激。 谢谢!

您放置的代码缺少该类的右括号。 要创建一个计数器,您只需创建一个私有全局变量

public rpsGUItest(){
    super("Rock, Paper, Scissors");
    int computerWins = 0;
    int userWins = 0;

并在相应区域(如果用户获胜)

else if(picker == 1.0){
            JOptionPane.showMessageDialog(null, "You picked Rock, the Computer picked Paper, the Computer Wins!, for some reason.");

            computerWins += 1;
        }

然后您可以在JLabel中显示它

您无法从actionPerformed方法中访问rspGUItest类中声明的字段,因为actionPerformed位于ActionListener接口中,而不是rpsGUItest中。 一种解决方案是将您的计数器声明为公共静态字段,并从回调方法中静态访问它们。

在rpsGUItest中:

public static int computerWins = 0;
public static int userWins = 0;

public rpsGUItest(){
    super("Rock, Paper, Scissors");

在ActionListener中:

else if(picker == 1.0){
            JOptionPane.showMessageDialog(null, "You picked Rock, the Computer picked Paper, the Computer Wins!, for some reason.");

            rpsGUItest.computerWins += 1;
        }

暂无
暂无

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

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