Java 为JButton提供多个鼠标点击选项(21点游戏)

Java 为JButton提供多个鼠标点击选项(21点游戏),java,swing,jbutton,actionlistener,blackjack,Java,Swing,Jbutton,Actionlistener,Blackjack,我正在使用JFrame编写一个21点程序,并试图使其尽可能简单。我的JButton、jbHit只需单击一次即可工作,但每次单击都会覆盖playersand和playerSide插槽。我希望它能与多次点击(3次点击-因为这是你在前两次发牌后可以得到的最大数量)选项一起工作,可以说,它应该对它们进行计数,以便阵列索引可以记录卡的图像。这是我到目前为止的ActionListener代码。恐怕我卡住了。我应该在int I++中使用某种for循环吗 //Hit Button ActionListener

我正在使用JFrame编写一个21点程序,并试图使其尽可能简单。我的JButton、jbHit只需单击一次即可工作,但每次单击都会覆盖playersand和playerSide插槽。我希望它能与多次点击(3次点击-因为这是你在前两次发牌后可以得到的最大数量)选项一起工作,可以说,它应该对它们进行计数,以便阵列索引可以记录卡的图像。这是我到目前为止的ActionListener代码。恐怕我卡住了。我应该在int I++中使用某种for循环吗

//Hit Button ActionListener
  jbHit.addActionListener( new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
           if ( playerValue < 21 ) {
              //Draw a card
              Card c = deck.drawCard();
              playersHand.add(c);
              playerSide[2].setIcon( new ImageIcon( c.getFilename() ) );
           }
           //If playerValue > 21, bust
           else if ( playerValue > 21 ) {
              //Toggle Buttons
              jbDeal.setEnabled(true);
              jbHit.setEnabled(false);
              jbStand.setEnabled(false);
              jbDoubleDown.setEnabled(false);
              message = "You bust.";
           }
        }
  });
//点击按钮ActionListener
jbHit.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
如果(播放值<21){
//抽牌
卡片c=卡片组.抽卡();
增补(c);
playerSide[2].setIcon(新的图像图标(c.getFilename());
}
//如果playerValue>21,则为半身像
否则如果(播放值>21){
//切换按钮
jbDeal.setEnabled(true);
jbHit.setEnabled(false);
jbStand.setEnabled(false);
jbDoubleDown.setEnabled(false);
message=“你破产了。”;
}
}
});

我相信您正在寻找基于整数的逻辑标志,例如:

if(cardsDealt < 3) { 
    // DoThings();
} else {
    return;
}
在按钮底部单击“句柄”


如果这不是你要问的,请重新解释这个问题

听起来您可能对jbHit的命名方式有点困惑。要知道,每次单击鼠标时,它都会被称为一个完整的时间。它并不是天生就知道它在第二次或第三次点击时。添加一个类成员,如int clickCount;在jbHit内部的适当点递增。然后,您可以根据clickCount的值更改方法的响应

您可以创建一个“动作命令”数组,每次单击按钮时,动作命令都会变为下一个。如果到达终点,请将索引设置回零。也许是这样的:

public static void main(String[] args)
{
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button = new JButton("Action");
    String[] commands = {"command1", "command2", "command3"};

    button.setActionCommand(commands[0]);
    button.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JButton btn = (JButton)e.getSource();

            String cmd = btn.getActionCommand();
            System.out.println("Command: " + cmd);

            if(cmd.equals("command1"))
            {
                btn.setActionCommand(commands[1]);
                System.out.println("Command 1 was pressed");
            }
            else if(cmd.equals("command2"))
            {
                btn.setActionCommand(commands[2]);
                System.out.println("Command 2 was pressed");
            }
            else if(cmd.equals("command3"))
            {
                btn.setActionCommand(commands[0]);
                System.out.println("Command 3 was pressed");
            }
            else
                System.out.println("Something went wrong!");
        }
    });
    panel.add(button);

    frame.add(panel);
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

如果您使用的是Java 7或更高版本,则可以使用Switch语句替换If/else。

您可能真正需要的是某种(或两种)模型,它可以维护有关当前状态、卡数及其数量、轮数的所有信息。你可以通过监听器与这些模型进行交互,并根据这些反馈改变这些状态…@MadProgrammer如果你想得到这种想象,你所需要的只是实现一个状态机,其中每个上下文都知道下一个操作。您可以使用状态模式来实现这一点。我最近设计了一个向导,使用状态模式来实现这一点。然而,OP希望它尽可能简单。@hfontanez只是另一种模型;)@我同意,但是OP不想得到那种幻想。这就是为什么我为状态机提供了一个糟糕的借口,创建了一个字符串数组作为操作命令,然后每次单击按钮都对其进行更改。我展示了一个状态机的“穷人”实现,通过创建一个字符串数组(动作命令),每次点击按钮,动作命令都会切换到下一个。所以,这是可以做到的。我并不是想暗示这是不可能做到的,只是为了帮助OP克服一个我认为可能会阻碍它的误解。还有,我喜欢你的状态机!好了,伙计们,今天电子课结束后,我会重新开始,我会尝试所有这些,看看哪一个效果最好。谢谢大家的时间和帮助。
public static void main(String[] args)
{
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton button = new JButton("Action");
    String[] commands = {"command1", "command2", "command3"};

    button.setActionCommand(commands[0]);
    button.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JButton btn = (JButton)e.getSource();

            String cmd = btn.getActionCommand();
            System.out.println("Command: " + cmd);

            if(cmd.equals("command1"))
            {
                btn.setActionCommand(commands[1]);
                System.out.println("Command 1 was pressed");
            }
            else if(cmd.equals("command2"))
            {
                btn.setActionCommand(commands[2]);
                System.out.println("Command 2 was pressed");
            }
            else if(cmd.equals("command3"))
            {
                btn.setActionCommand(commands[0]);
                System.out.println("Command 3 was pressed");
            }
            else
                System.out.println("Something went wrong!");
        }
    });
    panel.add(button);

    frame.add(panel);
    frame.setSize(200, 200);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}