Hi I am new to java and I am stuck on an assignemnt and im looking for some suggestions basically i have 8 buttons delcared as an array and i would like to double a number enetered in a textfield when clicking a certian button ..... i want each button to have diffrent values so ...how do i give a button a value ?

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

public class HorseRace extends JFrame implements ActionListener {
	
	
	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;
	
	
	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[1]))
		{
			//System.out.println("Got this far!!");
			x = Integer.valueOf(EnterAmount.getText());

			y = x + x ;

			DisplayAmount.setText(""+y);
		}
		
		
		
		
		
		
	}
	
	public static void main(String[]args)
	{
		HorseRace game = new HorseRace();
	}
	
	
	
}

You could make an array that coincides with the button array. Like this

double [] nums = {2.5,3,4.9,2,7.1,5,6,8,7}

and then compare the indexs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.