Java 如何将多个组件添加到 JFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4186835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add multiple components to a JFrame?
提问by rustybeanstalk
I have a JFrame.
我有一个JFrame。
I also have a Boxclass which extends Component. This box class has a paintmethod which makes a filled rectangle.
我还有一个Box类,它扩展了Component。这个盒子类有一个绘制方法,可以制作一个填充的矩形。
When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repainton 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!
提前致谢!
采纳答案by jjnguy
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()
在创建Box
. 这将告诉 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
. 而且,当您调用add
具有 BorderLayout 的组件时,该组件会自动添加到组件的中心。后续调用add
将覆盖中心组件,只在中间留下一个组件。
回答by Merky
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 构建器。
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
但是您必须明确地放置它们。真的取决于你想用它做什么。
回答by Kelly S. French
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 组件只允许向其中添加一个组件。我在使用选项卡式窗格时遇到过这个问题。每个选项卡只能接受一个控件(JPane?),因此您必须创建一个带有布局的单独面板来排列相关控件,然后将窗格作为一个单元添加到选项卡中。Swing 库中也有类似的安排。
回答by Bogdan
You could set the frame layout to null
and then use setBounds()
to position your boxes exactly where you want.
您可以将框架布局设置为null
,然后用于setBounds()
将您的盒子准确放置在您想要的位置。
回答by rustybeanstalk
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 Boxclass 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 的面板,将我的组件添加到该面板,它们都会正确显示。