text: (about java)a small question on this small piece of code...? in the code written here: http://en.wikibooks.org/wiki/java:swing how can i call a function when there is a mouse press/release for a button the code on wikibooks actually calls system.exit(0) when the exit button is pressed. the code segment is below          //creates a button on the calculator that can be used to close the program         jbutton buttonexit = new jbutton("exit"); //creates a keyboard shortcut of alt+c to use to close the program //instead of having to click on the button         buttonexit.setmnemonic(keyevent.vk_c);         buttonexit.addactionlistener(new actionlistener() {                 public void actionperformed(actionevent e) {                 system.exit(0);                  }         });         //adds the exit button         panel.add(buttonexit);  there are two ways of doing what you would like. firstly, you could copy the 'buttonexit.addactionlistener(..); part to every other button and make the function actionperformed in the new actionlistener do what you would like.  for instance, you could get it to write to the console with a line like system.out.println("you pressed the button xx"); depending on which button was pressed.  secondly, you could create a seperate actionlistener and implement in the actionperformed method checks to see which button was pressed.  note, this would need to be a seperate class.  for instance class fullactionlistener // the action listener for all buttons public void actionperformed(actionevent e) {     string command = e.getactioncommand();     if (command.equals("9")) {         // do whatever you need to here for the button 9.         system.out.println("you pressed button 9");     } else if (command.equals("8")) {         // etc     } }  then, actionlistener al = new fullactionlistener(); // the code on wikibooks         //creates all of the numbers that would be seen on a calculator         jbutton button1 = new jbutton("1");         button1.addactionlistener(al);         panel.add(button1);         jbutton button2 = new jbutton("2");         button2.addactionlistener(al);         panel.add(button2);  etc...