Java 如何在 Netbeans GUI Builder 中使用 CardLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21898425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:19:47  来源:igfitidea点击:

How to use CardLayout with Netbeans GUI Builder

javaswingnetbeanslayout-managercardlayout

提问by Paul Samsotha

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 Layoutfrom the context menu of a container component. Now I'm ready to rule the world!

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

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 Questionpage, "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!

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

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

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

采纳答案by Paul Samsotha

How to Use CardLayout

如何使用 CardLayout

  1. With a new JFrameform, add a JPanel, a few JButtonsto the form so it looks like this

    enter image description here

    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.

    enter image description here

  2. Now we se the layout of mainPanelto CardLayout. Double click the mainPanelin 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

    enter image description here

  3. Now we're going to add different JPanelsto the mainPanel. Just right click on the mainPanelfrom 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.

    enter image description here

  4. The layout part is set but lets add some labels so we can differentiate between the JPanelsand also change their card name. So double click panelOnefrom the navigator. You will see the panel in the design view. Just drag and drop a JLabelto 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.

    enter image description here

    We also want to change the name of the panels that were given as CardLayoutreferences. 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

    enter image description here

    Note: At any time, you can change the layout position, say you want panelTwoinitially shown, instead of panelOne. Just right click on mainPaneland 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 Onebutton. 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 showmethod.

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

    在此处输入图片说明

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

    在此处输入图片说明

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

    在此处输入图片说明

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

    在此处输入图片说明

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

    在此处输入图片说明

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

    在此处输入图片说明

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

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

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

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

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

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

enter image description here

在此处输入图片说明



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.

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

enter image description here

在此处输入图片说明