I have decided it was about time for me to learn how to make GUIs in java. Everything was coming along nicely until I began to set up my ActionListener.
Heres my actionListener class:
import java.awt.event.*;
public class Calculator implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == set)
{
setAllTheThings();
}
}
private void setAllTheThings()
{
System.out.println("hello");
}
}
What I cannot seem to do is get the actionListener to find the JButton called “set” in another class. How should I go about doing this?
Also, here’s said button:
JButton set = new JButton("Set");
set.setLocation(255,0);
set.setSize(50,20);
set.addActionListener(new Calculator());
line1.add(set);
You can implement the same code using
Actions– checking the source is not a good way to see the origin of the event.See: http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html