dimanche 8 mars 2015

VERY basic Slot Machine (checking to see if three labels are equal) Java

Here is a run down of the program:



  1. Three JLables are made.

  2. Three buttons are made corresponding to the labels.

  3. When a button is pressed it calls a random generator that picks a number between 0-9.

  4. Depending on what button is pressed the corresponding JLabel is changed to the number that was generated.

  5. Once all three numbers (in each label) are the same it automatically changes the main label to "You Win!".


Number 5 is the part I am having trouble with. I cant seem to be able to compare or tell when all three are the same. I would love if someine could point me in the right direction or provide any kind of help.


Here is the code for the action listener



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

public class SlotMachinePanel extends JPanel
{

private JRadioButton button1, button2, button3;
private JLabel instr, first, second, third;
private int check1, check2, check3;

//-----------------------------------------------------------------
// Constructor: Sets up the GUI.
//-----------------------------------------------------------------
public SlotMachinePanel()
{
// First Panel
instr = new JLabel ("Please spin a wheel");

JPanel one = new JPanel();
one.setPreferredSize (new Dimension(200,50));
one.setBackground(Color.green);
one.add(instr);

// Second Panel
button1 = new JRadioButton("Spin me!",false);
first = new JLabel ("1");

JPanel two = new JPanel();
two.setPreferredSize (new Dimension(200,50));
two.setBackground(Color.cyan);
two.add(first);
two.add(button1);

// Third Panel
button2 = new JRadioButton("Spin me!",false);
second = new JLabel ("2");

JPanel three = new JPanel();
three.setPreferredSize (new Dimension(200,50));
three.setBackground(Color.green);
three.add(second);
three.add(button2);


// Fourth Panel
button3 = new JRadioButton("Spin me!",false);
third = new JLabel ("3");

JPanel four = new JPanel();
four.setPreferredSize (new Dimension(200,50));
four.setBackground(Color.cyan);
four.add(third);
four.add(button3);


ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);

button1.addActionListener(new ButtonListener());
button2.addActionListener(new ButtonListener());
button3.addActionListener(new ButtonListener());

add(one);
add(two);
add(three);
add(four);

setPreferredSize(new Dimension(200, 210));
setBackground(Color.green);
}

//*****************************************************************
// listener for button pushes
//*****************************************************************
private class ButtonListener implements ActionListener
{

//--------------------------------------------------------------
//
//--------------------------------------------------------------
public void actionPerformed(ActionEvent event)
{
int num;

Random generator1 = new Random();
num = generator1.nextInt(9);

Object source = event.getSource();

if(source == button1){
check1 = num;
first.setText(Integer.toString(num));
}


if(source == button2){
check2 = num;
second.setText(Integer.toString(num));
}


if(source == button3){
check3 = num;
third.setText(Integer.toString(num));
}



if((check1 == check2) && (check2 == check3)){
instr.setText("You Win");
}
}
}
}


Its run using this



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

public class SlotMachine
{
//-----------------------------------------------------------------
// Creates the main program frame.
//-----------------------------------------------------------------
public static void main(String[] args)
{


JFrame frame = new JFrame("Slot Machine");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().add(new SlotMachinePanel());

frame.pack();
frame.setVisible(true);
}
}

Aucun commentaire:

Enregistrer un commentaire