Hi I am Making a horse racing application .... and the concept is when you enter a number in the textfield and select a horse(one of the 8 jButtons) the application will randomly select one of the JButtons and if the Jbutton is the same as the one selected by the user it will multply the value entered in the textfield by the value assigned to the Jbutton .
im stuck on how to randomly pick a Jbutton
import javax.swing.* ;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class HorseRace extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int x, y;
JPanel topPanel, gamePanel, bottomPanel;
JButton buttonHorse1, buttonHorse2, buttonHorse3, buttonhorse4, buttonHorse5, buttonHorse6,
buttonHorse7, buttonHorse8, buttonPlaceBet;
JTextField EnterAmount , DisplayAmount;
private JButton buttons[];
private int button_count = 8;
private Random r = new Random();
public HorseRace(){
super("Guessing Game");
setup();
setSize(1000, 500);
setVisible(true);
}
public void setup(){
Container c = getContentPane();
setUpPanels();
setUpButtons();
SetUpTextFields();
buttonPanelAdd();
addActions();
//Set Content Pane Layout
c.add(topPanel, BorderLayout.NORTH);
c.add(gamePanel, BorderLayout.CENTER);
c.add(bottomPanel, BorderLayout.SOUTH);
}
public void setUpPanels(){
topPanel = new JPanel();
gamePanel = new JPanel();
bottomPanel = new JPanel();
//Set Grid Layout
gamePanel.setLayout(new GridLayout(2, 40, 40, 4));
}
public void setUpButtons(){
buttons = new JButton[button_count];
for (int i=0; i < button_count;i++)
{
buttons[i] = new JButton(" ");
}
buttonPlaceBet = new JButton("Place Bet");
}
public void SetUpTextFields(){
EnterAmount = new JTextField("Enter Bet");
DisplayAmount = new JTextField("Your Winnings ");
}
public void buttonPanelAdd(){
for (int i=0; i<button_count; i++)
{
gamePanel.add(buttons[i]);
}
bottomPanel.add(buttonPlaceBet);
bottomPanel.add(EnterAmount);
bottomPanel.add(DisplayAmount);
}
public void addActions(){
for (int i=0; i<button_count; i++)
{
buttons[i].addActionListener(this);
}
buttonPlaceBet.addActionListener(this);
EnterAmount.addActionListener(this);
DisplayAmount.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(buttons[0]))
{
//System.out.println("Got this far!!");
x = Integer.valueOf(EnterAmount.getText());
}
}
public static void main(String[]args)
{
HorseRace game = new HorseRace();
}
}