开发者

How to add multiple components to a JFrame?

I have a JFrame.

I also have a Box class which extends Component. This box class has a paint method which makes a filled rectangle.

When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repaint on the JFrame.

I took a look at the layout managers, but I am not sure that's what I want. All I want is to be able to make an animation of whole bunch of rectangles wherever I want on the screen.

(I also tried creating a panel, adding the panel to the JFrame, and then adding all the Box components to the panel. This did not work either).

Thanks in 开发者_开发技巧advance!


You have 2 choices.

You can change the layout of your frame:

JFrame frame;
frame.setLayout(new FlowLayout());

Now, if you add more than one box, it will show up on the frame.

The other option is to do what you said you tried. (Adding a panel to the frame)

JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')

Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.

You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.

And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.


You do need to check out other layout managers. JFrame by default uses BorderLayout and without specifying the "place" a component is added, they get added to CENTER. Depending on what you want your UI to look like depends on the layout manager to use. I would suggest maybe using Netbeans GUI builder.

EDIT: Missed the part about what you want to add but the concept is still the same, if you just add these components to the default layout manager, they will get overwritten. Sounds like you may need to do your painting inside of just one of your Box components or create a JPanel and set the layout to null but then you would have to place them explicitly. Really depends on what you want to do with it exactly.


Do your layout on paper first, then read up on Swing layout managers.

Be aware that some Swing components only allow one component to be added to them. I've run across this when using Tabbed panes. Each tab can only accept one control (JPane?) so you have to create a separate panel with a layout to arrange the related controls and then as a unit add the pane to the tab. There are similar arrangements in the Swing library.


You could set the frame layout to null and then use setBounds() to position your boxes exactly where you want.


Thank you for all your answers.

Since I am using my own custom class, Box, I have the ability of setting the position of my the rectangle through the paint method.

I realized my Box class was extending the wrong thing. It should have been extending javax.swing.Jcomponent.

If I now use a panel with an OverlayLayout, add my components to that panel, they all show up properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜