princejose
asked on
DrawString( ) not happening in Frame Window
Hi,
I am a beginner with Java programming. Got a few stoppers here. Here is my program
import java.awt.*;
public class BaseFrame2 extends Frame
{
TextDialog dlg;
String message = "This is a Window";
BaseFrame2(String title)
{
super(title);
setFont(new Font("Helvetica",Font.BOLD ,12));
dlg = new TextDialog(this,"Enter Text",true);
dlg.resize(150,100);
MenuBar mb = new MenuBar();
Menu m = new Menu("Colors");
m.add(new MenuItem("Red"));
m.add(new MenuItem("Green"));
m.add(new MenuItem("Blue"));
m.add(new MenuItem("-"));
m.add(new CheckboxMenuItem("Black")) ;
Menu mf= new Menu("Font");
mf.add(new MenuItem("Regular"));
mf.add(new MenuItem("Bold"));
mf.add(new MenuItem("Italic"));
m.add(mf);
m.add(new MenuItem("Set Text"));
mb.add(m);
setMenuBar(mb);
}
public void paint(Graphics g)
{
g.drawString(message,20,20 );
}
From my main applet class on a Button Click Action I am popping up the above defined Frame. But I am not able see the message "This is a window" drawn into the Frame. What could be wrong? I tried even explicitly calling repaint()
in the constructor. But still I am not able to see the message drawn into my Frame Window. Why is this happening? Hope one of you there can help me..
Is there any settings I have to give to see texts drawn into my Frame.
Thank you very much..
Prince
I am a beginner with Java programming. Got a few stoppers here. Here is my program
import java.awt.*;
public class BaseFrame2 extends Frame
{
TextDialog dlg;
String message = "This is a Window";
BaseFrame2(String title)
{
super(title);
setFont(new Font("Helvetica",Font.BOLD
dlg = new TextDialog(this,"Enter Text",true);
dlg.resize(150,100);
MenuBar mb = new MenuBar();
Menu m = new Menu("Colors");
m.add(new MenuItem("Red"));
m.add(new MenuItem("Green"));
m.add(new MenuItem("Blue"));
m.add(new MenuItem("-"));
m.add(new CheckboxMenuItem("Black"))
Menu mf= new Menu("Font");
mf.add(new MenuItem("Regular"));
mf.add(new MenuItem("Bold"));
mf.add(new MenuItem("Italic"));
m.add(mf);
m.add(new MenuItem("Set Text"));
mb.add(m);
setMenuBar(mb);
}
public void paint(Graphics g)
{
g.drawString(message,20,20
}
From my main applet class on a Button Click Action I am popping up the above defined Frame. But I am not able see the message "This is a window" drawn into the Frame. What could be wrong? I tried even explicitly calling repaint()
in the constructor. But still I am not able to see the message drawn into my Frame Window. Why is this happening? Hope one of you there can help me..
Is there any settings I have to give to see texts drawn into my Frame.
Thank you very much..
Prince
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
at 20,20 the String is probably being hidden inside the insets somewhere. try what falter said and see if that works.
Kylar
Kylar
princejose,
like falter said, you are not setting the size of the frame at all.
preferably, call pack() in the constructor ( does the laying out & calculation of sizes). But for this, you need to add components (not use paint(). For example
BaseFrame2()
{
//previous code
//add new WhateverDisplay();
}
class WhateverDisplay
{
public void paint(Graphics g)
{
//blah blah
}
public Dimension getPreferredSize()
{
//return size u want( preferably calculate it dynamically)
}
like falter said, you are not setting the size of the frame at all.
preferably, call pack() in the constructor ( does the laying out & calculation of sizes). But for this, you need to add components (not use paint(). For example
BaseFrame2()
{
//previous code
//add new WhateverDisplay();
}
class WhateverDisplay
{
public void paint(Graphics g)
{
//blah blah
}
public Dimension getPreferredSize()
{
//return size u want( preferably calculate it dynamically)
}
try to pring in another x,y axis
for example 100,100
and see what happen
for example 100,100
and see what happen
ASKER
Hi Falter,
I was of the impression that the pixels are counted from beneath the menubar. But what you said is correct. When I changed the position of the string it is visible.
Thank you very much. Thanks to all who had taken their time to respond to my query.
Prince
I was of the impression that the pixels are counted from beneath the menubar. But what you said is correct. When I changed the position of the string it is visible.
Thank you very much. Thanks to all who had taken their time to respond to my query.
Prince
fyi from the api
validate
public void validate()
Validates this container and all of its subcomponents.
AWT uses validate to cause a container to lay out its subcomponents again after the components it contains have been
added to or modified.
Overrides:
validate in class Component
See Also:
validate(), Component.invalidate()
-Philip