java - Adding JPanel to JFrame with FlowLayout -
i trying display filled oval on screen using subclass of jpanel when try add object of subclass jframe flowlayout oval not displayed correctly don't know problem is. me please?
here's code
@override protected void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.black); g.filloval(0, 0, 50, 50); }
in main
jframe frame = new jframe("ball"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(300,300); frame.setvisible(true); frame.setlocationrelativeto(null); frame.setlayout(new flowlayout()); ballpanel ball = new ballpane(); frame.add(ball);
see javadoc of flowlayout
:
a flow layout lets each component assume natural (preferred) size
since custom painting, ballpanel
cannot compute preferred size (which based on components add panel). override in ballpanel
class getpreferredsize
method return correct dimensions (correct means matching custom painting). consider doing same getminimumsize
, getmaximumsize
.
what use when have debug swing layout problems add borders components in specific colors. way see size components take in parent container, might starting point debugging. in case, show tiny panel ;-)
Comments
Post a Comment