Java Reference
In-Depth Information
Defining the AboutDialog Class
You derive your own dialog class from JDialog so you can create an About dialog. AboutDialog seems
like a good name for the new class.
The constructor for the AboutDialog class needs to accept three arguments — the parent Frame object,
which is the application window in Sketcher; a String object defining what should appear on the title bar;
and a String object for the message that you want to display. There is only one button in the dialog window,
an OK button to close the dialog. You can make the whole thing self-contained by making the AboutDialog
class the action listener for its OK button, and because it's relevant only in the context of the Sketcher-
Frame class, you can define it as a nested class to SketcherFrame :
// Import statements as before...
public class SketcherFrame extends JFrame {
// SketcherFrame class as before...
// Class defining a general purpose message box
class AboutDialog extends JDialog implements ActionListener {
public AboutDialog(JFrame parent, String title, String message) {
super(parent, title, true);
// If there was a parent, set dialog position inside
if(parent != null) {
Dimension parentSize = parent.getSize(); // Parent size
Point p = parent.getLocation(); // Parent position
setLocation(p.x + parentSize.width/4,p.y + parentSize.height/4);
}
// Create the message pane
JPanel messagePane = new JPanel();
messagePane.add(new JLabel(message));
getContentPane().add(messagePane);
// Create the button pane
JPanel buttonPane = new JPanel();
JButton button = new JButton("OK");
// Create OK button
buttonPane.add(button);
// add to content pane
button.addActionListener(this);
getContentPane().add(buttonPane, BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
// Size window for components
setVisible(true);
}
// OK button action
public void actionPerformed(ActionEvent e) {
setVisible(false);
// Set dialog invisible
dispose();
// Release the dialog resources
}
}
Search WWH ::




Custom Search