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

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

问题描述

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

Preface (this is a self-answer post)

我已经厌倦了Netbeans GUI Builder,但我只是没有开始潜入它来学习更复杂的细节。我真的甚至不知道从设计视图更改布局管理器,我只是手工编码。因此,我尝试通过询问如何在Netbeans GUI Builder中使用不同的布局管理器来询问Google服务台,并在结果的前几页中惊奇地发现了zilch。在Eclipse Window Builder中,您可以从调色板中拖放不同的布局管理器,为什么不在GUI Builder中。瞧,经过几个小时的搜索,我从容器组件的上下文菜单中找到了神奇的设置布局。现在我准备好统治世界了!

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!

我想我会介绍一些关于如何使用GUI Builder中的不同布局管理器的教程,这里也是如此,所以其他人不会秃头撕裂他们的头发试图计算我自己一直在想什么。完成 CardLayout (下面)的第一个教程后,我准备发布我的努力并输入提问题页面的标题,如何在Netbeans GUI Builder中使用CardLayout什么...... !! 。关于这个话题已经有一些问题!!我想我应该让我的Google查询更精确。 DOHH!

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!

无论如何,我现在有了这个教程,它比其他答案中提供的更有信息,所以我的努力​​不会浪费(所以我告诉我自己:D)。也许我会制作一系列这些东西。走着瞧。现在,享受如何使用CardLayout :P

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

推荐答案

如何使用CardLayout



How to Use CardLayout


  1. 使用新的 JFrame 表格,添加 JPanel ,表格中有一些 JButtons ,所以它看起来像这样

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




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

现在我们来看 mainPanel 到 CardLayout 。双击导航器中的 mainPanel ,使其在设计视图中自行显示。然后在导航器中右键单击它并选择设置布局 - > CardLayout 。您的导航器现在应该如下所示

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

现在我们要在中添加不同的 JPanels mainPanel中。只需右键单击导航器中的 mainPanel ,然后选择从调色板添加 - > Swing Containers - > JPanel 。这样做三次,所以你有三个不同的 JPanels 。我也改变了他们的变量名。你的导航器不应该是这样的。

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.

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

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.




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



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




注意:您可以随时更改布局位置,例如您想要 panelTwo 最初显示,而不是 panelOne 。只需右键单击 mainPanel ,然后选择更改订单。您可以在订单上向上或向下移动面板。



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.

我们差不多完成了。我们只需要将按钮添加到按钮以在 CardLayout 中的面板之间切换。所以双击导航器中的框架。你现在应该看到按钮。右键单击 Panel One 按钮。并选择活动 - >行动 - >的actionPerformed 。您应该在源代码视图中看到自动生成的代码。添加这段代码

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.

如果你按照上面的5个步骤,您的程序应该如下运行。

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

也可以将其他类JPanel表单类拖放到您的 mainPanel ,如果您还有其他想要使用的人。对于更大的非平凡案例,这可能是一种首选方法,以避免大量的类别。

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天全站免登陆