Java Reference
In-Depth Information
// Tell the frame how to lay out components you add.
getContentPane().setLayout(new FlowLayout());
// Add the button to the frame.
getContentPane().add(button);
}
6. Override the actionPerformed() method in SimpleFrame . SimpleFrame and,
by extension, MyFrame are listeners for action events. The create-Button()
method added the ActionListener in the button creation process, so any
action taken on the button (that is, clicking it) automatically calls this
method. If you had multiple buttons, the action command would allow
you to distinguish which button had been clicked.
public void actionPerformed(ActionEvent e)
{
// Get the action command.
String command = e.getActionCommand();
// Pop up a message when the button is clicked.
if (command.equals("Click me"))
informationMessage("I've been clicked", "Click Message");
}
7. Finally, code a main method that can be executed.
static public void main(String [] args)
{
new MyFrame();
}
8. Compile MyFrame and execute it. A window entitled “My Frame” should
pop up with a single button centered in the upper part of the window.
When you click the button, the message “I've been clicked” should appear.
9. The file SimpleFrame.java has had Javadoc comments added to it. As a final
task, go to the java4cobol directory and key in the following command:
➔javadoc SimpleFrame.java
If your system paths are set up correctly, the Javadoc should run and cre-
ate HTML that documents the SimpleFrame class. With your browser, open
the index.html file that was created in the java4cobol directory and exam-
ine the output.
Search WWH ::




Custom Search