在Java GUI中创建计数器

时间:2014-08-04 12:56:04

标签: java swing user-interface counter

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

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

2 个答案:

答案 0 :(得分:1)

您提供的代码缺少该类的结束括号。要制作一个计数器,您只需创建一个私有全局变量

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中显示它

答案 1 :(得分:1)

您无法从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;
        }