java - How to add 1 drawing with a button to JFrame ? -
my drawing class: example want draw simple line
public class drawnot1 extends jpanel { private basicstroke bs = new basicstroke(2); private int x; private int y; public drawnot1(int x, int y){ setsize(100, 100); this.x = x; this.y = y; } @override protected void paintcomponent(graphics g){ super.paintcomponent(g); dodrawing(g); } private void dodrawing(graphics g){ graphics2d g2d = (graphics2d) g; g2d.setstroke(bs); g2d.drawline(x, y, x, y+10); }
my jframe class:
public class main extends jframe{ private int x; private int y; public main() { initui(); } public void initui() { setsize(600, 500); settitle("points"); setlocationrelativeto(null); setdefaultcloseoperation(jframe.exit_on_close); add(new drawnot1(20, 20)); add(new jbutton("button1")); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { @override public void run() { main ex = new main(); ex.setvisible(true); } }); }
}
i want show drawing next button doesn't appear component appear button, drawing not.
my ultimate goal when press button drawing appear near button.
jframe
uses borderlayout
default, adding 2 components default (centre
) position means last 1 added shown.
try adding button south
position instead
add(new jbutton("button1"), borderlayout.south);
you may find overriding getpreferredsize
method of drawdot1
, providing suitable value result in better output
Comments
Post a Comment