Hello all =)
Does anyone know if there is a method that will take in a JButton's label as a string? I've looked through the java API everywhere and can't seem to find one that'll do the job. What I'm doing is making a simple calculator that, when a number button is clicked, it will invoke an ActionListener method which will toss the appropriate number into the text field. But instead of creating a new ActionListener method for each number, I'd like to make it cleaner and just make a single one that'll do all the jobs. Hence the method that returns a JButton label. A short example of what I'm doing is below. If anyone has any other suggestions, I'm open to them =)
Thanks!
Jisoo22
Does anyone know if there is a method that will take in a JButton's label as a string? I've looked through the java API everywhere and can't seem to find one that'll do the job. What I'm doing is making a simple calculator that, when a number button is clicked, it will invoke an ActionListener method which will toss the appropriate number into the text field. But instead of creating a new ActionListener method for each number, I'd like to make it cleaner and just make a single one that'll do all the jobs. Hence the method that returns a JButton label. A short example of what I'm doing is below. If anyone has any other suggestions, I'm open to them =)
Thanks!
Jisoo22
Code:
//JButton construction
JButton zeroButton = new JButton("0");
zeroButton.setPreferredSize(new Dimension(40, 20));
zeroButton.addActionListener(new numListener());
....
//ActionListener method
class numListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String str = ??? //Take in label here
displayField.setText(str);
num1 = 0;
}
}