如何将多个组件添加到JFrame?

时间:2023-01-28 23:04:03

I have a JFrame.

我有一个JFrame。

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

我还有一个扩展Component的Box类。这个box类有一个paint方法,它可以生成一个填充的矩形。

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.

当我将多个这些Box组件添加到我的JFrame时,当我在JFrame上调用repaint时,只显示最近添加的一个。

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).

(我也尝试创建一个面板,将面板添加到JFrame,然后将所有Box组件添加到面板。这也不起作用)。

Thanks in advance!

提前致谢!

5 个解决方案

#1


9  

You have 2 choices.

你有2个选择。

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.

另外,你应该小心你的盒子的大小。你可能想要在创建Box的某个地方调用setPreferredSize()。这将告诉Java在添加到布局时框的大小。

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

您还应该查看Java布局管理器教程。那里有很多很棒的信息。

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.

还有一件事。原因是框架上一次只显示一个框是因为JFrame的布局管理器是BorderLayout。并且,当您在具有BorderLayout的组件上调用add时,该组件会自动添加到组件的中心。后续调用add将覆盖中心组件,中间只留下一个组件。

#2


1  

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

您可以将框架布局设置为null,然后使用setBounds()将框准确定位到所需的位置。

#3


0  

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.

您需要查看其他布局管理器。默认情况下,JFrame使用BorderLayout并且未指定添加组件的“地点”,它们将添加到CENTER。取决于您希望UI的外观取决于要使用的布局管理器。我建议也许使用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.

编辑:错过了关于你要添加的内容的部分,但概念仍然相同,如果你只是将这些组件添加到默认布局管理器,它们将被覆盖。听起来你可能需要在你的一个Box组件中进行绘画,或者创建一个JPanel并将布局设置为null,然后你必须明确地放置它们。真的取决于你想要用它做什么。

#4


0  

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

首先在纸上进行布局,然后在Swing布局管理器上阅读。

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.

请注意,某些Swing组件只允许将一个组件添加到它们中。我在使用Tabbed窗格时遇到过这个问题。每个选项卡只能接受一个控件(JPane?),因此您必须创建一个单独的面板,其中布局用于排列相关控件,然后作为一个单元将窗格添加到选项卡。 Swing库中有类似的安排。

#5


0  

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.

由于我使用自己的自定义类Box,我可以通过paint方法设置矩形的位置。

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

我意识到我的Box类正在扩展错误的东西。它应该一直在扩展javax.swing.Jcomponent。

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

如果我现在使用具有OverlayLayout的面板,将我的组件添加到该面板,它们都会正确显示。

#1


9  

You have 2 choices.

你有2个选择。

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.

另外,你应该小心你的盒子的大小。你可能想要在创建Box的某个地方调用setPreferredSize()。这将告诉Java在添加到布局时框的大小。

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

您还应该查看Java布局管理器教程。那里有很多很棒的信息。

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.

还有一件事。原因是框架上一次只显示一个框是因为JFrame的布局管理器是BorderLayout。并且,当您在具有BorderLayout的组件上调用add时,该组件会自动添加到组件的中心。后续调用add将覆盖中心组件,中间只留下一个组件。

#2


1  

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

您可以将框架布局设置为null,然后使用setBounds()将框准确定位到所需的位置。

#3


0  

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.

您需要查看其他布局管理器。默认情况下,JFrame使用BorderLayout并且未指定添加组件的“地点”,它们将添加到CENTER。取决于您希望UI的外观取决于要使用的布局管理器。我建议也许使用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.

编辑:错过了关于你要添加的内容的部分,但概念仍然相同,如果你只是将这些组件添加到默认布局管理器,它们将被覆盖。听起来你可能需要在你的一个Box组件中进行绘画,或者创建一个JPanel并将布局设置为null,然后你必须明确地放置它们。真的取决于你想要用它做什么。

#4


0  

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

首先在纸上进行布局,然后在Swing布局管理器上阅读。

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.

请注意,某些Swing组件只允许将一个组件添加到它们中。我在使用Tabbed窗格时遇到过这个问题。每个选项卡只能接受一个控件(JPane?),因此您必须创建一个单独的面板,其中布局用于排列相关控件,然后作为一个单元将窗格添加到选项卡。 Swing库中有类似的安排。

#5


0  

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.

由于我使用自己的自定义类Box,我可以通过paint方法设置矩形的位置。

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

我意识到我的Box类正在扩展错误的东西。它应该一直在扩展javax.swing.Jcomponent。

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

如果我现在使用具有OverlayLayout的面板,将我的组件添加到该面板,它们都会正确显示。