Search code examples
javaswingjbuttonactionlistenerblackjack

give JButton multiple options for mouse clicks (Blackjack Game)


I am writing a Blackjack program using JFrame and trying to keep it as simple as possible. My JButton, jbHit works with a single click, however it overwrites the playersHand and playerSide slot with every click. I would like it to work with multiple clicks (3 clicks - since that is the max number of cards you can get after the first two are dealt) options It should count them so to speak so that the array index can record the card image. Here is my ActionListener code that I have so far. I am afraid I am stuck. Should I use some sort of for loop with an int i++?

//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.";
           }
        }
  });

Solution

  • You could create an array of "action commands" and every time you click the button, the action command changes to the next. If you reach the end, set the index back to zero. Perhaps something like this:

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

    If you are using Java 7 or later, you can replace the if/else with a Switch statement.