• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dialog example not working

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you have some other class called Point in your classpath. Try changing line 51 to read:
 
Bob Young
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct, missed it. Thanks again.
 
reply
    Bookmark Topic Watch Topic
  • New Topic