在java中如何将一个JPanel添加到另一个JPanel
我希望添加 3 个 JPanel,它们之间的距离不同,填充内容窗格/JFrame。 目前,add(new JPanel()) 将覆盖当前内容窗格或根本不显示。
理想情况下,我希望代码类似于(例如,为了):
JPanel panel1 = new JPanel();
panel1.add(new jbutton())
panel1.setBounds(0,0,100,100);
JPanel panel2 = new JPanel();
panel2.add(new jbutton());
panel2.setBounds(100,0,100,100);
getContentPane().add(panel1);
getContentPane().add(panel2);
等等...
我知道可以将面板添加到面板,因为我已经使用 borderlayout 完成了它。 请不要告诉我使用布局管理器,我一直在使用 gridbag,但它没有产生我想要的结果。
我想完全控制我的对象的大小和位置。另外,请不要告诉我,“你很蠢;这很愚蠢;你为什么要这样做?”
请只提供有用的回复!我不是菜鸟程序员,只是一个对 GUI 感到沮丧的人,所以请随意深入了解您想要的深度。
I wish to add 3 JPanels, all spaced different distances apart, that fill the Content Pane/JFrame.
Currently, add(new JPanel()) will overrite the current content pane or not show up at all.
Ideally I want the code to resemble (for example's sake):
JPanel panel1 = new JPanel();
panel1.add(new jbutton())
panel1.setBounds(0,0,100,100);
JPanel panel2 = new JPanel();
panel2.add(new jbutton());
panel2.setBounds(100,0,100,100);
getContentPane().add(panel1);
getContentPane().add(panel2);
Etc...
I know a panel can be added to a panel because I've done it with borderlayout.
Please don't tell me to use a layout manager, I've been using gridbag and it hasn't produced the results I want.
I would like full control over my objects' size and placement. Also, please don't tell me, "you're dumb; this is stupid; why would you ever want to do that?"
Only helpful responses please! I'm not a noob programmer, just a guy frustrated with GUIs, so feel free to go into as deep a depth as you'd like.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JFrame 的内容窗格默认使用 BorderLayout,因此每次向其中添加新面板时,如果不指定其在布局中的位置,则会将其添加到中心,从而替换前一个面板。
只需将内容窗格的布局设置为您想要的即可。您当然可以使用空布局(即将布局设置为
null
),但有时您会被这个设计错误所困扰。仅仅因为您无法使用布局管理器并不意味着您不应该使用。继续学习。 GridBagLayout 是最复杂的布局之一。你可以先用其他的。The content pane of a JFrame uses a BorderLayout by default, so each time you add a new panel to it, without specifying its location in the layout, it adds it to the center, and thus replaces the previous one.
Just set the content pane's layout to what you want. You may of course use the null layout (i.e. set the layout to
null
), but you'll be bitten by this design mistake some time. Just because you've not been able to use layout managers doesn't mean that you shouldn't. Just keep learning. GridBagLayout is one of the most complex ones. You could use other ones first.在您的示例中,面板之间的距离没有不同。这些面板相互连接。因此,您需要做的就是使用带有水平间隙为 0 的 FlowLayout 的面板。
您的主要代码可能类似于:
鉴于您发现这样的布局代码令人困惑,我建议您确实不明白如何使用布局管理器。和其他人一样,我建议您花时间了解如何使用布局管理器来发挥您的优势。
顺便说一句,使用空布局并不会让你的生活变得更轻松。当然,您可以将组件准确地放置在您想要的位置,但是您是否满足面板占据内容窗格的全部空间的要求?您将无法使用框架的 pack() 方法,因为当您使用空布局时,您的“主”面板将没有首选大小,因此当您使框架可见时,您将看到的只是标题栏和边框。如果您手动尝试设置框架的大小,那么您知道 3 个面板的大小 (300, x 100),但您不知道标题栏和边框的大小。所以你将无法正确计算尺寸。
了解布局管理器可能需要花费几分钟的时间,但这是非常值得的。
In you example the panels are not spaced different distances apart. The panels connect to one another. So all you need to do us use a panel with a FlowLayout that uses a horizontal gap of 0.
Your main code can be something like:
Given that you find layout code like this confusing, I would suggest you truly don't understand how to use layout managers. And like everybody else I suggest you take the time to understand how to use layout managers to your advantage.
By the way using a null layout does NOT make you life any easier. Sure you can place the components exactly where you want,but you do you satisfy your requirement that the panels take up the full space of the content pane? You won't be able to use the pack() method of the frame because when you use a null layout, your "main" panel won't have a preferred size so when you make the frame visible all you will see is the title bar and borders. If you manually try to set the size of the frame then yes you know the size of the 3 panels (300, x 100) but you don't know the size of the title bar and borders. So you won't be able to calculate the size properly.
It may take a few more minutes to understand layout managers but it is well worth it.
有一个关于不使用布局管理器的很好的教程,应该会有所帮助你。它提出的最重要的建议是使用 IDE,例如 NetBeans,但它也涵盖了手动执行操作。
请注意,如果您找不到适合您的默认 LayoutManager,您始终可以编写自己的布局管理器 - 尽管在本例中它听起来像 JSplitPane 是您所需要的 - 请参阅 如何使用分割窗格
There's a good tutorial on Doing Without a Layout Manager that should help you. The most important recommendation it makes is to either use an IDE such as NetBeans, but it also covers doing things by hand.
Note that if you can't find a default LayoutManager that works for you, you can always write your own one - although in this case it sounds like JSplitPane is what you need - see How to Use Split Panes
布局管理器将允许您完全控制组件的放置和大小,您只需要找到一个对于您的应用程序来说足够简单的布局管理器,同时为您提供所需的控制量。没有布局管理器是可能的,但我永远不会建议用这种方式编写 GUI 代码,你最终会管理太多的 GUI 东西,这些东西通常由 LayoutManager 正确管理(例如免费的空间管理、父窗口调整大小..)。
大多数
LayoutManager
类都以相同的方式工作;当您将组件添加到Container
时,您还指定了一个约束,它将告诉LayoutManager
将组件放置在哪里,以及如何处理所有类型的事件对组件放置的影响(例如调整容器的大小)。一些布局管理器具有非常简单的约束(BorderLayout
仅询问组件位置),而其他布局管理器则具有一组非常完整的约束(GridBagLayout
,通过GridBagConstraints< /code> 类)
我更喜欢的
LayoutManager
是GridBagLayout
(JavaDoc)。正确使用 GridBagContraints 类时,您可以指定小部件的显示位置、每个组件之间的间距、可用空间的分配方式等。此 教程 应该可以帮助您开始使用此布局管理器。如果这个不能满足您的需求,或者您觉得它太复杂,您应该在网上搜索有关其他
LayoutManager
的教程。Layout managers will allow you to fully control your component placement and size, you just need to find the one that is simple enough for your application while giving you the amount of control you need. Doing without a layout manager is possible, but I would never suggest such a way of writing GUI code, you will end up managing way too many GUI stuff which are usually correctly managed by a
LayoutManager
(such as free space management, parent window resize..).Most of the
LayoutManager
classes work in the same way; when you add a component to aContainer
, you also specify a constraint, which will tell theLayoutManager
where to place the component, and how to handle all type of events that has an influence on the component placement (such as resizing the container). Some layout managers have very simple constraints (BorderLayout
only asks for the component location), whereas others have a very complet set of constraints (GridBagLayout
, via theGridBagConstraints
class)The
LayoutManager
I prefer is theGridBagLayout
(JavaDoc). When correctly using theGridBagContraints
class, you can specify where your widgets are displayed, what spacing there is to be between each component, how free space is distributed, etc.. This tutorial should help you getting started with this layout manager.If this one does not suit your needs, or if you feel it is too complex, you should search the web for tutorials on other
LayoutManager
.