如何在 Netbeans GUI Builder 中使用 CardLayout [英] How to use CardLayout with Netbeans GUI Builder

查看:20
本文介绍了如何在 Netbeans GUI Builder 中使用 CardLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前言(这是一篇自我回答的帖子)

我已经对 Netbeans GUI Builder 感兴趣了,但我还没有开始深入研究它以了解更复杂的细节.我真的不知道从设计视图中更改布局管理器的热度,我只会手动编码.因此,我尝试通过询问如何在 Netbeans GUI Builder 中使用不同的布局管理器" 来询问 Google 帮助台,并且在结果的前几页中意外地发现了 zilch.在 Eclipse Window Builder 中,您可以从调色板拖放不同的布局管理器,那么为什么不在 GUI Builder 中呢.瞧,经过数小时的搜索,我从容器组件的上下文菜单中找到了神奇的 Set Layout.现在我已经准备好统治世界了!

我想我会在一些教程中介绍如何使用 GUI Builder 中的不同布局管理器,所以其他人不会秃头试图弄清楚我一直在为自己弄清楚什么.在完成关于 CardLayout(下面)的第一个教程后,我准备发布我的努力并在 Ask Question 页面的标题中输入,How to use带有 Netbeans GUI 生成器的 CardLayout" .什么......!!.已经有一些关于这个话题的问题了!!我想我应该让我的 Google 查询更精确.天啊!

无论如何,我现在有了这个教程,它仍然比其他答案中提供的教程提供更多信息,所以我的努力​​不会白费(所以我告诉自己:D).也许我会做一系列这些 tuts.我们拭目以待.现在,享受如何使用 CardLayout :P

解决方案

如何使用 CardLayout

  1. 使用一个新的 JFrame 表单,在表单中添加一个 JPanel 和几个 JButtons 使其看起来像这样



    您的导航器窗格应如下所示.请注意,我更改了变量名称.您可以通过右键单击导航器中的组件并选择更改变量名称来完成此操作.

  2. 现在我们将mainPanel的布局设置为CardLayout.双击导航器中的 mainPanel,使其在设计视图中单独可见.然后在导航器中右键单击它并选择 Set Layout -> CardLayout.您的导航器现在应如下所示

  3. 现在我们要向 mainPanel 添加不同的 JPanels.只需在导航器中右键单击 mainPanel 并选择 从调色板添加 -> Swing Containers -> JPanel.这样做三遍,这样你就有了三个不同的 JPanels.我还更改了它们的变量名称.您的导航器不应如下所示.

  4. 布局部分已设置,但让我们添加一些标签,以便我们可以区分JPanels 并更改它们的卡片名称.因此,从导航器中双击 panelOne.您将在设计视图中看到该面板.只需将一个 JLabel 拖放到它上面,然后将标签的文本编辑到 Panel One.对另外两个也这样做,相应地命名它们的标签.完成后,您的导航器应如下所示.



    我们还想更改作为 CardLayout 引用给出的面板的名称.我们可以通过双击其中一个面板 (panelOne) 并转到属性窗格来实现.在底部,您将看到一个属性 Card Name.只需将其更改为您想要的任何内容,我使用了 panelOne.对另外两个 JPanel

    这样做



    注意:您可以随时更改布局位置,假设您希望 panelTwo 最初显示,而不是 panelOne.只需右键单击 mainPanel 并选择 Change Order.您可以根据订单上下移动面板.

  5. 我们快完成了.我们只需要为按钮添加监听器即可在 CardLayout 中的面板之间切换.所以从导航器双击框架.你现在应该看到按钮了.右键单击 Panel One 按钮.并选择 Events ->动作 ->actionPerformed.您应该会在源代码视图中看到自动生成的代码.添加这段代码

    private void jbtPanelOneActionPerformed(ActionEvent evt) {CardLayout card = (CardLayout)mainPanel.getLayout();card.show(mainPanel, "panelOne");}

    对其他两个按钮执行此操作,确保将相应面板的正确名称传递给 show 方法.

如果您已按照上述 5 个步骤进行操作,您的程序应如下运行.

<小时>

如果您想使用其他类,也可以将其他类 JPanel 表单类拖放到您的 mainPanel 上.这可能是较大的非平凡案例的首选方法,以避免庞大的类.

Preface (this is a self-answer post)

I've gotten my feet wet with Netbeans GUI Builder but I am just not starting to dive in to it to learn the more intricate details. I really didn't even know hot to change layout manager from the design view, I would just hand code it. So I tried asking the Google help desk by asking "How to use different layout managers in Netbeans GUI Builder" and surprisingly found zilch in the first couple pages of the results. In Eclipse Window Builder, from the palette you can drag an drop different layout managers so why not in GUI Builder. Lo and behold, after hours of searching, I found the magical Set Layout from the context menu of a container component. Now I'm ready to rule the world!

I figured I throw in some tutorials on how to use different layout managers from GUI Builder, here on SO so others won't go bald tearing their hair out trying to figure out what I what I've been figuring out for myself. After completing the first tutorial on CardLayout (below) I get ready to post my efforts and type in to the title of the Ask Question page, "How to use CardLayout with Netbeans GUI Builder" . What the ...!!. There were already some question asked on this topic!!. I guess I should've made my Google query more precise. DOHH!

Anyway, I have this tutorial now, which is still more informative than the ones provided in other answers, so my efforts will not have been wasted (so I tell myself :D ). Maybe I'll make a series of these tuts. We'll see. For now, enjoy How to use CardLayout :P

解决方案

How to Use CardLayout

  1. With a new JFrame form, add a JPanel, a few JButtons to the form so it looks like this



    Your navigator pane should look like this. Notice I changed the variable names. You can do that by right clicking on the component from the navigator and selecting change variable name.

  2. Now we se the layout of mainPanel to CardLayout. Double click the mainPanel in the navigator, so it's visible by itself in the design view. Then right click it in the navigator and select Set Layout -> CardLayout. Your navigator should now look like this

  3. Now we're going to add different JPanels to the mainPanel. Just right click on the mainPanel from the navigator and select Add from Palette -> Swing Containers -> JPanel. Do that three times so you have three different JPanels. I also changed their variable names. Your navigator should not look like this.

  4. The layout part is set but lets add some labels so we can differentiate between the JPanels and also change their card name. So double click panelOne from the navigator. You will see the panel in the design view. Just drag and drop a JLabel to it and edit the text of the label to Panel One. Do that for the other two also, naming their labels accordingly. When you're done, your navigator should look like this.



    We also want to change the name of the panels that were given as CardLayout references. We can do that by double clicking on one of the panel (panelOne) and going to the properties pane. There towards the bottom, you will see a property Card Name. Just change it to whatever you want, I used panelOne. Do that for the other two JPanel



    Note: At any time, you can change the layout position, say you want panelTwo initially shown, instead of panelOne. Just right click on mainPanel and select Change Order. You can move the panels up or down on the order.

  5. We're almost done. We just need add the listeners to the buttons to switch between panels in the CardLayout. So double click on the frame from the navigator. You should see the buttons now. Right click on the Panel One button. and select Events -> Action -> actionPerformed. You should see auto-generated code in the source code view. Add this piece of code

    private void jbtPanelOneActionPerformed(ActionEvent evt) {                                            
        CardLayout card = (CardLayout)mainPanel.getLayout();
        card.show(mainPanel, "panelOne");
    } 
    

    Do this for the other two buttons, making sure to pass the correct name of the corresponding panel to the show method.

If you've followed the 5 steps above, your program should run as follows.


It's also possible to drag and drop other class JPanel form classes onto your mainPanel, if you have others you'd like to use. This may be a preferred approach for bigger non-trivial cases, to avoid humungous classes.

这篇关于如何在 Netbeans GUI Builder 中使用 CardLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆