【问题标题】:Coin flip simulation: Counting heads / tails硬币翻转模拟:计数正面/反面
【发布时间】:2019-01-07 04:24:52
【问题描述】:

我正在尝试使用 Java 和窗口构建器来模拟投币器。当按下该按钮时,我有一个名为“翻转”的按钮,硬币图像会根据我创建的随机生成器生成的数字而变化。

我现在正试图找出一种方法来显示硬币在各自的JTextFields 中出现正面或反面的次数。我正在考虑使用计数器,但我正在为如何将其更新到文本字段中而苦苦挣扎,到目前为止,它只表明我将每个硬币都翻转了一次。

我对编程非常陌生,因此非常感谢任何建议或指导。

// this button flips the coin
btnFlip = new JButton("Flip");
btnFlip.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        int headCounter = 0;
        int tails = 0; 
        // this implements the random flip of the coin when the checkbox Run Multiple
        // flips is unchecked
        if (chckbxNewCheckBox.isSelected() == false) {

            Random r = new Random();
            int flipper = r.nextInt(2);
            if (flipper == 1) { 
                lblImages.setIcon(new ImageIcon(FinalPrep.class.getResource("/finalPrep/heads.png")));
                textFieldHeads.setText(String.valueOf(headCounter));

            } else {
                lblImages.setIcon(new ImageIcon(FinalPrep.class.getResource("/finalPrep/tails.png")));
                textFieldTails.setText(String.valueOf(tails));
            }
        }
    }
});
btnFlip.setFont(new Font("Tahoma", Font.PLAIN, 15));
panel_1.add(btnFlip);

【问题讨论】:

  • 请发布您的一些代码。没有它就很难给出答案。
  • 大概是这样:声明两个整数数据类型的类字段,一个名为HEADS,另一个名为TAILS。这些将被视为全局类,将两者都初始化为 0。随机应为 0 或 1,并在单击按钮时生成。如果随机值 = 0 那么TAILS++;。如果随机值 = 1 那么HEADS++;。 JTextField 很可能是 JLabels,因为它们仅用于显示目的,而且在最后但在按钮单击事件中:headsTextField.setText(String.valueOf(HEADS));tailsTextField.setText(String.valueOf(TAILS));
  • 嗯,@DevilsHnd 我也有同样的想法,也许我没有正确编写所有代码。我将发布到目前为止的内容。
  • 好主意@AmieJensen。这样我们就可以看到问题可能潜伏在哪里。
  • @DevilsHnd 好的,我认为可能会起作用,在我将 textField 设置为计数器后,我最初拥有 headCounter++;但它只是将heads文本字段增加到一个并且即使我一次又一次按下翻转也保持这种状态

标签: java swing jbutton jtextfield


【解决方案1】:

您必须创建类似于 2 层的东西。一个包含窗口本身和标题(把它想象成一个受体)然后你必须创建另一个包含按钮和文本字段的“层”。像这样的:

import javax.swing.*;

public class MyFrame extends JFrame {

    private JPanel panel;
    private JTextField textField;
    private JButton button;

    public MyFrame(){

        panel = new JPanel(); //Step 1. Creation of a receptor

        tfCount = new JTextField(10); //Step 2.
        button = new JButton("Press Me"); //Creation of buttons & textfields.

        panel.add(tfCount); //Step 3.
        panel.add(button); //Add those graphics to the receptor  

        this.setContentPane(panel); //Step 4 Adjust the receptor to the object

        this.setVisible(true);
        this.setSize(400, 400);
        this.setTitle("My 1st GUI!");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
} 

(对不起,随机示例,但您没有发布任何代码示例,所以我试图尽可能地解释)

然后你应该根据生成的数字放置一个“标志”,例如:

    boolean flag = true; //Flag

    int counter = 0; //The counter for the heads

    int x; //The number you received from the generator

    int counter_b; //The counter for the tails

    if(x%2 == 0){ //If the number is even
        flag = false; // You set down the flag
        counter++; //And counter raises its value
    }
    else
        counter_b++;


    tfCount.setEditable(true); //This makes your textfield editable
    tfCount.setText(String.valueOf(counter)); //And this prints the counter's value

请记住,反面和正面的计数器只是让您了解如何使用“标志”的示例。我希望我能够帮助你,因为我也是新手!! :)

【讨论】:

  • 比你厉害多了@Stavros
  • @amiej33 如果此答案解决了您的问题,请考虑通过单击左侧的复选标记将其标记为已接受的答案。
猜你喜欢
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-14
  • 2010-10-03
相关资源
最近更新 更多