pack() 方法的问题
我正在构建一个 GUI 应用程序,在 JFrame 中我有 2 个 jcombobox 和一个 JPanel 来查看某些数据。现在,当我在主类中调用 pack() 方法时,它将两个 jcombobox 放在我的 JPanel 旁边,这是我不想要的,因为我希望它们向北。当然,我尝试在代码中对其进行硬编码,但在调用 pack() 方法后它不起作用。 这种方法还有其他选择吗?
I'm building a GUI application, and within a JFrame i have 2 jcombobox's and a JPanel to view certain data. Now when i call the pack() methode in the main class it puts the two jcombobox'es next to my JPanel, which i dont want, because I want them North. Ofcourse I've tried to hard-code it in my code, but it doesn't work after I've called the pack() method.
Are there any alternatives to this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只有一个组件可以是 NORTH,因此如果您希望两个 ComboBox 都为 NORTH,则必须将它们添加到一个单独的容器中。然后可以将这个单独的容器放在北方。
(发布来源以获得更准确的帮助。)
Only one component can be NORTH, so if you want both ComboBoxes to be NORTH you have to add them into a separate container. This separate container can then be put NORTH.
(Post the source for more exact help.)
pack
所做的只是将Window
(在本例中为JFrame
)的大小调整为其首选大小及其子组件的首选大小。要控制子组件相对于彼此的实际位置,您需要使用适当的布局管理器。您可能需要查看使用布局管理器教程。
All
pack
does is resize theWindow
(in this caseJFrame
) to its preferred size and the preferred sizes of its sub-components. To control the actual location of the sub-components relative to one another you need to use an appropriateLayoutManager
.You might want to check out the Using Layout Managers tutorial.
pack()
方法只是导致布局发生,它与放置什么内容完全无关。您很可能没有正确使用布局管理器。向我们展示您的代码,我们可以告诉您到底做错了什么。
The
pack()
method just causes the layouting to happen, it has abolutely nothing to do with what is put where.Most likely you're not using layout managers correctly. Show us your code and we can tell you waht exactly you're doing wrong.
您可以通过使用 setSize 和 setBounds 显式设置帧大小来避免使用 pack。然而,使用 pack 通常是首选方式,因为它让框架布局管理器负责框架大小。
话虽这么说,您所描述的问题似乎与布局管理器的正确使用有关,而不是与框架的大小有关。查看 Swing 的各种布局管理器以及如何使用它们: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html。
You can avoid using pack by explicitly setting the frame size with setSize and setBounds. However, using pack is usually the preferred way as it leaves the frame layout manager in charge of the frame size.
That being said, the problem you are describing appears to be related to the correct use of a layout manager rather than the sizing of the frame. Have a look at the various layout managers for Swing and how to use them: http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/layout/using.html.