|
interface ActionListener { void actionPerformed(ActionEvent e); // Called when Mouse click OR // when ENTER key pressed } |
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ButtonPressAppl { public static void main(String[] args) { JFrame f = new JFrame("Button Test"); JButton B; B = new JButton("Press me !"); f.getContentPane().add(B); // Add button f.setSize(300, 300); f.setVisible(true); } } |
public static class myListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.out.println("The button was press !");
}
}
|
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ButtonPressAppl { public static void main(String[] args) { JFrame f = new JFrame("Button Test"); JButton B; B = new JButton("Press me !"); f.getContentPane().add(B); // Add button B.addActionListener(new myListener() ); // Register ActionListen Interface class f.setSize(300, 300); f.setVisible(true); } public static class myListener implements ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("The button was press !"); } } } |