Question

I am really stuck on this. I know the other pieces work, but the buttons don't show the update on the graphic interface, and they cannot be static. Just hit a wall. Any help is highly appreciated!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlotMachinePanel extends JPanel{

private JPanel buttonPanel, newSlotsPanel, primary;
protected int tokens = 5;
protected int score = 0;
private String[] resultOfSpin = {"Winner", "Loser"};
public slotPane sPane = new slotPane();


public SlotMachinePanel(){

    newSlotsPanel = new JPanel();
    newSlotsPanel.setPreferredSize(new Dimension(300, 100));
    newSlotsPanel.setBackground(Color.WHITE);
    JLabel slotLabel = new JLabel("Current Tokens: " + getTokens() + "  Result of Spin: " + sPane.getScore());
    newSlotsPanel.add(slotLabel);
    newSlotsPanel.add(sPane);

    buttonPanel = new JPanel();
    buttonPanel.setPreferredSize(new Dimension(300, 50));
    buttonPanel.setBackground(Color.BLUE);

        JButton spin = new JButton ("Spin!");
        spin.addActionListener (new spinButton());

        JButton cashOut = new JButton ("Cash Out");
        cashOut.addActionListener (new cashOutButton());

        JLabel label = new JLabel ("Spin to play!");

        setPreferredSize (new Dimension(300, 40));
        setBackground (Color.WHITE);

    buttonPanel.add(spin);
    buttonPanel.add(cashOut);
    buttonPanel.add(label);

    primary = new JPanel();
    primary.setPreferredSize(new Dimension(350, 200));
    primary.setBackground(Color.RED);
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));


    primary.add(newSlotsPanel);
    primary.add(buttonPanel);
    add(primary);
}

public int getTokens(){ return tokens; }

public void setTokens(int x){ tokens += x; }

public int getScore(){ return sPane.getScore(); }

public void setScore(int x){ sPane.setScore(x); }

public void spinSlot(){ sPane = new slotPane();}

public void spinPressed(){
    setTokens(-1);
    sPane.spin(); 
    setTokens(getScore()+sPane.getScore());
}

private class spinButton implements ActionListener{
  public void actionPerformed (ActionEvent event){
     //this.setText("Spun!");
     spinPressed();         
     if(SlotMachinePanel.getTokens() <= 0){
         JOptionPane.showMessageDialog(null, "Spun Out! \n You have " + SlotMachinePanel.getTokens() + " tokens!");
            System.exit(0);   
     }
  }
}

private class cashOutButton implements ActionListener{
  public void actionPerformed (ActionEvent event){
     JOptionPane.showMessageDialog(null, "Cashed Out! \n You get " + SlotMachinePanel.getTokens() + " tokens!");
     System.exit(0);
  }
}
}
Was it helpful?

Solution

In your ActionListener of your spinButton you make a static call on getTokens(). You need to change that to SlotMachinePanel.this.getTokens(). This way you access the current instance of SlotMachinePanel in the ActionListener. The same goes for the ActionListener on cashOutButton.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top