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