|
Learning objectives:After completing this project, you should be able to:
Items in red are learning objectives for this part of the project. |
Group work
| BasicGUIObjects report
| Time limit
|
BasicGUIObjects
project and your BasicGUIObjectsReport.txt
file. Add a heading to your report to indicate the beginning of Part 2.ActionListener
InterfaceBasicGUIObjects
class, anywhere after the exit button has been created:
exitButton.addActionListener( this );
addActionListener
method must be an ActionListener
, but this refers to a BasicGUIObjects
object. Add the clause implements ActionListener
to the header of the BasicGUIObjects
class definition. You'll need the following import
statement, even though you should already be importing java.awt.*
:
import java.awt.event.*
ActionListener
interface, the actionPerformed
method must be present, and it will be invoked whenever an event occurs for which the object is registered as a listener. In our case, it will be invoked whenever the exit button is clicked. Add the following method to the BasicGUIOjbects
class:
public void actionPerformed( ActionEvent e ) { System.out.println ( "That tickles" ); }
System.exit( 0 );
AddPanel
constructor so that each AddPanel
object will be an ActionListener
for the JButton
object that it contains. Have it display a message on the console whenever an event occurs. instanceof
operatorAddPanel
object as an ActionListener
for its JTextField
as well.ActionEvent
parameter of the actionPerformed
method can provide information about the event that caused the method to be invoked. In particular, its getSource
method returns the object that generated the event (e.g. the button or the text field). In order to find out what kind of object it is, we can use the instanceof
operator. Replace the current body of the actionPerformed
method with the following:
if( e.getSource() instanceof JTextField ) {
System.out.println (
"That tickles my JTextField"
);
} else {
System.out.println (
"That tickles my JButton"
);
"The Acrobat's name will be " + ((JTextField) e.getSource() ).getText()
JTextField
a field, and replace ((JTextField) e.getSource() )
with the name of the field. AcrobatPanel
class:
public void addAcrobat( String theName ) {
JButton acrobat = new JButton( theName );
this.add( acrobat );
}
BasicGUIObjects
class with a field, and add the following method:
public void addAcrobat( String theName ) { basicPanel.addAcrobat( theName ); }
main
method: mainFrame.addAcrobat( "FirstBasic" );
addAcrobat
method in the AcrobatPanel
class: this.validate();
BasicGUIObjects
field to the AddPanel
class. BasicGUIObjects
parameter to the AddPanel
constructor. actionPerformed
method so that it calls the addAcrobat
method of the object's BasicGUIObjects
field, using the contents of the text field as the parameter. BasicGUIObjects2
.addActionListener
method.ActionListener
interface.actionPerformed
method.instanceof
operator.