When a user clicks on a button, an ActionEvent object is delivered to the button. In order for the button to perform an application specific task, we must register an ActionEventListener to the button and program this event listener to perform the task we want. The ActionListener class is an example of what is called a "command" from the Command Design Pattern. The code to add an ActionListener to a button looks like:
myButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // code to perform a task goes here... } });
The example Frame2 shows how to dynamically change the layout manager for a JFrame by calling its setLayout(...) method resulting in a dynamic re-arrangement of the GUI components in the
JFrame.
- 瀏覽次數:1215