简体   繁体   English

在java中,如何将JPanel添加到另一个JPanel

[英]In java how do you add a JPanel to another JPanel

I wish to add 3 JPanels, all spaced different distances apart, that fill the Content Pane/JFrame. 我希望添加3个JPanel,它们间隔不同的距离,填充Content Pane / JFrame。 Currently, add(new JPanel()) will overrite the current content pane or not show up at all. 目前,add(new JPanel())将覆盖当前内容窗格或根本不显示。

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. 我知道可以将面板添加到面板中,因为我已经使用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. 请不要告诉我使用布局管理器,我一直在使用gridbag,它没有产生我想要的结果。

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. 我不是一个菜鸟程序员,只是一个对GUI感到沮丧的人,所以请随意深入了解你想要的深度。

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. 默认情况下,JFrame的内容窗格使用BorderLayout,因此每次向其添加新面板时,如果未在布局中指定其位置,则会将其添加到中心,从而替换前一个面板。

Just set the content pane's layout to what you want. 只需将内容窗格的布局设置为您想要的。 You may of course use the null layout (ie set the layout to null ), but you'll be bitten by this design mistake some time. 你当然可以使用null布局(即将布局设置为null ),但是有时你会被这个设计错误所困扰。 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. GridBagLayout是最复杂的一个。 You could use other ones first. 你可以先使用其他的。

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. 它最重要的建议是使用NetBeans之类的IDE,但它也包括手工操作。

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 请注意,如果找不到适合您的默认LayoutManager,您可以随时编写自己的版本 - 尽管在这种情况下听起来像JSplitPane就是您所需要的 - 请参阅如何使用拆分窗格

I wish to add 3 JPanels, all spaced different distances apart, that fill the Content Pane/JFrame. 我希望添加3个JPanel,它们间隔不同的距离,填充Content Pane / JFrame。

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. 所以你需要做的就是使用一个FlowLayout面板,它使用水平间隙0。

Your main code can be something like: 您的主要代码可以是:

JPanel main = new JPanel( new FlowLayout(FlowLayout.CENTER, 0, 0) );

JPanel panel1 = new JPanel();
panel1.setPreferredSize( new Dimension(100, 100) );
panel1.add( new JButton() );
main.add( panel1 );

JPanel panel2 = new JPanel();
panel2.setPreferredSize( new Dimension(100, 100) );
panel2.add( new JButton() );
main.add( panel2 );

frame.add( main );

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. 顺便使用null布局并不会让你的生活变得更轻松。 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. 您将无法使用框架的pack()方法,因为当您使用空布局时,“主”面板将没有首选大小,因此当您使框架可见时,您将看到的所有内容都是标题酒吧和边界。 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. 如果您手动尝试设置框架的大小然后是,您知道3个面板(300,x 100)的大小,但您不知道标题栏和边框的大小。 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. 可能需要几分钟才能理解布局管理器,但这非常值得。

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..). 没有布局管理器是可能的,但是我永远不会建议这样编写GUI代码的方法,你最终会管理太多的GUI内容,这些内容通常由LayoutManager正确管理(例如可用空间管理,父窗口调整大小)。 )。

Most of the LayoutManager classes work in the same way; 大多数LayoutManager类以相同的方式工作; when you add a component to a Container , you also specify a constraint, which will tell the LayoutManager 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). 将组件添加到Container ,还要指定一个约束,该约束将告诉LayoutManager放置组件的位置,以及如何处理对组件放置有影响的所有类型的事件(例如调整容器大小)。 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 the GridBagConstraints class) 一些布局管理器具有非常简单的约束( BorderLayout仅询问组件位置),而其他布局管理器具有非常完整的约束集( GridBagLayout ,通过GridBagConstraints类)

The LayoutManager I prefer is the GridBagLayout ( JavaDoc ). LayoutManager我更喜欢的是GridBagLayout的JavaDoc )。 When correctly using the GridBagContraints 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. 正确使用GridBagContraints类时,您可以指定窗口小部件的显示位置,每个组件之间的间距,可用空间的分布方式等。本教程可帮助您开始使用此布局管理器。

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 . 如果这个不适合您的需求,或者您觉得它太复杂,您应该在网上搜索其他LayoutManager教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM