I was looking over an example given for the Dialog class in
Java Class Libraries by Patrick Chan, Rosanna Lee. I can't get past the errors. Any help would be appreciated.
The example demonstrates how to center a dialog with respect to the parent frame. The message that is displayed in the message dialog is the current time.
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
class JCLap510 extends Frame implements ActionListener {
Button b = new Button("See Current Time");
JCLap510() {
b.addActionListener(this);
add(b,BorderLayout.NORTH);
pack();
show();
}
public void actionPerformed(ActionEvent e) {
String time = DateFormat.getTimeInstance(DateFormat.LONG).format(new Date());
new MessageDialog(this, "the time is now - "+time);
}
public static void main(String[] s) {
new JCLap510();
}
class MessageDialog extends Dialog implements ActionListener {
MessageDialog(Frame f, String msg) {
super(f, "Message", true);
Button b = new Button("OK");
Label lb = new Label(msg, Label.CENTER) {
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
return new Dimension(d.width+40, d.height+40);
}
};
b.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
} );
add(lb, BorderLayout.NORTH);
add(b, BorderLayout.SOUTH);
pack();
Dimension myDim = getSize();
Dimension frameDim = f.getSize();
Dimension screenSize = getToolkit().getScreenSize();
Point loc = f.getLocation();
// center the dialog with respect to the frame.
loc.translate((frameDim.width-myDim.width)/2, (frameDim.height-myDim.height)/2);
loc.x = Math.max(0, Math.min(loc.x, screenSize.width-getSize().width));
loc.y = Math.max(0, Math.min(loc.y, screenSize.width-getSize().height));
setLocation(loc.x, loc.y);
show();
}
public void actionPerformed(ActionEvent e) {
dispose();
}
}
}
--------------------------- Compiler Output ---------------------------
JCLap510.java:51: Incompatible type for declaration. Can't convert java.awt.Point to Point.
Point loc = f.getLocation();
^
JCLap510.java:54: Method translate(int, int) not found in class Point.
loc.translate((frameDim.width-myDim.width)/2, (frameDim.height-myDim.height)/2);
^
JCLap510.java:57: Incompatible type for method. Explicit cast needed to convert double to int.
setLocation(loc.x, loc.y);
^
JCLap510.java:57: Incompatible type for method. Explicit cast needed to convert double to int.
setLocation(loc.x, loc.y);
^
4 errors
I think all the problems cascade from the error at line 51 Point loc = f.getLocation();
however I do not know why this would be an error.