java - Displaying a JLabel text on top of a drawing -
i can't figure out how make work.
i created main class, extended in jframe. if run program draw() , paint() methods draw something, label not visible. if run main() class without draw() , paint() methods, works.
i'm trying playing around swing components hard (i'm such noob t.t).
would me, please?
i'd see label on top of rectangle.
public class main extends jframe { jlabel label; public main() { setsize(400, 300); settitle("title"); setdefaultcloseoperation(exit_on_close); setlocationrelativeto(null); setvisible(true); label = new jlabel("text inside"); add(label, borderlayout.north); } public void paint(graphics g){ image img = createimage(getwidth(), getheight()); graphics gr = img.getgraphics(); draw(gr); g.drawimage(img, 0, 0, this); } public void draw(graphics g){ g.drawrect(100, 100, 200, 100); } public static void main(string[] args) { main start = new main(); } }
couple of things:
move addition of label before showing jframe , add label contentpane instead of jframe directly.
currently image overlaying because width , height of image equals frame. not sure trying achieve drawing blank image rectangle. can directly draw rectangle on jframe itself.
if intend set image background , on want show label need differently. need create jlabel desired image , add contentpane.
hope these pointers you.
Comments
Post a Comment