WindowBuilder如何使用其他按钮来显示计数器

问题描述 投票:0回答:1

我有一个使用Eclipse在Windowbuilder中创建的程序。我希望程序生成一个随机数,用户必须猜测它是偶数还是奇数。我已经基于命令行创建了游戏,但是我想做的是使用GUI进行游戏。我遇到的一个问题是何时要结束游戏,并让用户使用退出按钮查看得分。但是,当我尝试显示最终分数的正确性和不正确性时,我无法做到,因为它采用的是不同的方法。因此,我希望在单击时显示一个单独的按钮以显示对与错。这是代码:

 import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
public class RandomNumber {

    private JFrame frame;
    private JTextField textFieldMe;
    private JTextField textFieldCpu;
    private JTextField textFieldScore;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    RandomNumber window = new RandomNumber();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public RandomNumber() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        textFieldMe = new JTextField();
        textFieldMe.setBounds(152, 62, 86, 20);
        frame.getContentPane().add(textFieldMe);
        textFieldMe.setColumns(10);

        textFieldCpu = new JTextField();
        textFieldCpu.setBounds(152, 118, 86, 20);
        frame.getContentPane().add(textFieldCpu);
        textFieldCpu.setColumns(10);

        textFieldScore = new JTextField();
        textFieldScore.setBounds(152, 217, 86, 20);
        frame.getContentPane().add(textFieldScore);
        textFieldScore.setColumns(10);

        JButton btnGo = new JButton("GO!");/* this is the start button*/
        btnGo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Random rand = new Random();
                int correctCount = 0;
                int incorrectCount = 0;

                int ran = rand.nextInt(50) + 1;
                String me = textFieldMe.getText();
                if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
                    correctCount++;
                }else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
                    incorrectCount++;
                }

                textFieldCpu.setText(Integer.toString(ran));

                int totalRight = correctCount;
                int totalWrong = incorrectCount;
                String right = Integer.toString(totalRight);
                String wrong = Integer.toString(totalWrong);


            }
        });
        btnGo.setBounds(149, 149, 89, 23);
        frame.getContentPane().add(btnGo);

        JButton btnQuit = new JButton("Quit"); /* i want this button to display the score*/
        btnQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String right = Integer.toString(totalRight);
                String wrong = Integer.toString(totalWrong);

                textFieldScore.setText("Correct & wrong: " + right + wrong ); 
            }
        });
        btnQuit.setBounds(321, 216, 89, 23);
        frame.getContentPane().add(btnQuit);

        JLabel lblNewLabel = new JLabel("Your Number");
        lblNewLabel.setBounds(167, 36, 135, 14);
        frame.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("CPU Guess");
        lblNewLabel_1.setBounds(162, 93, 71, 14);
        frame.getContentPane().add(lblNewLabel_1);

        JLabel lblScore = new JLabel("Score");
        lblScore.setBounds(179, 192, 46, 14);
        frame.getContentPane().add(lblScore);
    }
}
java eclipse windowbuilder swingbuilder
1个回答
0
投票

我试图通过将rightwrong转换为类中的变量来使每个方法可见。新的类看起来像

public class RandomNumber {
  ...
  private int right, wrong;
  ...
  private void initialize() {
    ...
    btnGo.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Random rand = new Random();
        int ran = rand.nextInt(50) + 1;

        String me = textFieldMe.getText();
        if (ran % 2 == 0 && me.equals("e") || ran % 2 != 0 && me.equals("o")){
          ++right;
        }else if (ran % 2 == 0 && me.equals("o") || ran % 2 != 0 && me.equals("e")){
          ++wrong;
        }

        textFieldCpu.setText("" + ran);
      }
    }
    ...
    btnQuit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        textFieldScore.setText("Correct : " + right + " Wrong: " wrong );
        //will automatically convert ints to String
      }
    }
    ...
  }
  ...
}
© www.soinside.com 2019 - 2024. All rights reserved.