java - How to add multiple components to a JFrame? -
i have jframe.
i have box class extends component. box class has paint method makes filled rectangle.
when add multiple of these box components jframe, added 1 displayed when call repaint on jframe.
i took @ layout managers, not sure that's want. want able make animation of whole bunch of rectangles wherever want on screen.
(i tried creating panel, adding panel jframe, , adding box components panel. did not work either).
thanks in advance!
you have 2 choices.
you can change layout of frame:
jframe frame; frame.setlayout(new flowlayout());
now, if add more 1 box, show on frame.
the other option said tried. (adding panel frame)
jpanel pane = new jpanel(); frame.add(pane); (add boxes 'pane')
also, should careful sizing of box
. want call setpreferredsize()
somewhere in creation of box
. tell java size make box when added layout.
you should take @ java layout manager tutorials. there lots of great info there.
and, 1 more thing. reason 1 box @ time being displayed on frame because jframe's layout manager borderlayout
. and, when call add
on component has borderlayout, component automatically added center of component. subsequent calls add
overwrite center component, leaving 1 component in middle.
Comments
Post a Comment