multiple button listeners

I got the button for one to work however I am a bit stumped on how to get the other buttons. Can someone lead me in the right direction as to how to create a button event listener for the other buttons? I want the text to display in the same JLabel.

JButton one =new JButton ("1");

one.addActionListener(new ButtonListener());

JButton two =new JButton ("2");

two.addActionListener(new ButtonListener());

JButton three =new JButton ("3");

JButton four =new JButton ("4");

JButton five =new JButton ("5");

JButton six =new JButton ("6");

JButton seven =new JButton ("7");

JButton eight =new JButton ("8");

JButton nine =new JButton ("9");

JButton asterisk =new JButton ("*");

JButton zero =new JButton ("0");

JButton sharp =new JButton ("#");

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(asterisk);

add(zero);

add(sharp);

}

privateclass ButtonListenerimplements ActionListener

{

publicvoid actionPerformed(ActionEvent event)

{

display.setText("1");

}

}

[2327 byte] By [pberardi1a] at [2007-11-27 11:21:01]
# 1

you've got an array of buttons, so make it an array. You can use a String array to hold the labels, then loop from i = 0; i < labels.count or something like that. add the same actionlistener and look at actioncommand. that will get you your label and let you decide what to do with the button press

petes1234a at 2007-7-29 14:46:06 > top of Java-index,Java Essentials,New To Java...
# 2

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class BunchOfBtns extends JPanel

{

private String[] labels = {"1", "2", "3", "4", "5", "6", "7", "8",

"9", "*", "0", "#"};

JButton[] buttons;

JLabel display = new JLabel();

public BunchOfBtns()

{

super(new GridLayout(0, 3, 6, 6));

setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));

buttons = new JButton[labels.length];

for (int i = 0; i < labels.length; i++)

{

buttons[i] = new JButton(labels[i]);

add(buttons[i]);

buttons[i].addActionListener(new ButtonListener());

}

add(display);

}

private class ButtonListener implements ActionListener

{

public void actionPerformed(ActionEvent ae)

{

String actnCmd = ae.getActionCommand();

display.setText(actnCmd);

}

}

public static void main(String[] args)

{

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

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

frame.pack();

frame.setVisible(true);

}

}

petes1234a at 2007-7-29 14:46:06 > top of Java-index,Java Essentials,New To Java...