Edit: Just noticed the topic is a duplicate of this one
. Please don't post duplicate topics.
I've showed you how to use the same ActionListener for multiple buttons in the code below, but there are problems with this code I think you should fix:
1. A Swing application should not be run on the same thread that runs the main() method. You can find out more about proper Swing programming here
2. An entire Swing GUI should not be run from the main() method. You may have created this runnable example in a hurry and chose the design for simplicity, but the design shown in the code you posted should not be your typical Swing application design. The main() method should be one or two lines long with the bulk of the code in the primary Swing class, often creating a JFrame, starting with its constructor.
3. Your next question might be about programming your "timers" to keep track of the actual time you had in mind. For that you'll want to learn about the javax.swing.Timer class.
4. You might also wonder about having multiple timers running at the same time. I haven't worked out that answer completely. Multiple Timer instances might be sufficient or, if not, then a SwingWorker for each button (or button press) might be appropriate. Let me know if you'd like to work on that.
Keep coding, and good luck!
public class BlueRedTimer
public static void main(String args)
JFrame frame=new JFrame("Test");
JPanel panel=new JPanel();
JButton button=new JButton("Timer1");
JButton button2=new JButton("Timer2");
JButton button3=new JButton("Timer3");
JButton button4=new JButton("Timer4");
final class Action implements ActionListener
public void actionPerformed(ActionEvent e)
// if a single ActionListener is to handle multiple events, then
// add source discrimination
Object sourceActionCmd = e.getActionCommand();
if ( sourceActionCmd.equals( "Timer1" ) )
System.out.println( "Button Timer1 was selected.");
setTime = 300; // I want the "blue" timer to be set at 5 minutes.
// So when i press button timer1 it will start a
// countdown of 5 minutes and then a screen pops up.
System.out.println( setTime );
// the if statement here originally is unnecessary
JOptionPane.showMessageDialog(null, "Timer one is ready!");
else if ( sourceActionCmd.equals( "Timer2" ) )
// and so on . . .
System.out.println( "Button Timer2 was selected.");
// and so on . . .