如何刷新NetBeans中的自定义bean? [英] How to refresh custom bean in NetBeans?

查看:152
本文介绍了如何刷新NetBeans中的自定义bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NetBeans IDE中设计一个相对复杂的UI.我相信将所有这些组件整合到一个面板中会增加维护该面板的难度.

I want to design a relatively complex UI in NetBeans IDE. I believed that bring all these components together in one Panel can intensify difficulty of maintenance of such Panel.

为此,我在扩展了JPanel的多个自定义bean中分解了此结构.在主面板中,我通过调色板工具栏中的 Choose Bean 按钮将它们包括在内.

For this purpose I've broken down this structure in multiple custom beans extending JPanel. And in the main Panel I've included them by Choose Bean button in the palette toolbar.

每件事看起来都不错,但是当我更改其中一个子bean时,更改不会在主面板中导入的自定义bean上进行.

Every things looks OK but when I change one of the child beans, the change does not take place on the imported custom bean in main Panel.

请指出我如何重新加载该bean?谢谢

Please point me out how can I reload that bean? Thanks

推荐答案

为此,我在多个自定义中细分了此结构 Bean扩展了JPanel.在主面板中,我将它们包括在内 在调色板工具栏中选择Bean按钮.

For this purpose I've broken down this structure in multiple custom beans extending JPanel. And in the main Panel I've included them by Choose Bean button in the palette toolbar.

作为一个入门者,我想说这是一个非常有效的方法,对于未来的访问者,您正在按照所述的步骤进行操作此处将面板添加到主面板.可以通过拖放操作完成相同的操作将bean直接从Projects资源管理器拖放到Design视图.

Just as a starter I'd like to say it is a perfectly valid apporach and for future visitors, you are following the steps described here to add your panels to the main panel. The same can be done via drag & drop the bean directly from the Projects explorer to the Design view.

每件事看起来都不错,但是当我更换其中一个子豆时, 更改不会在主面板中导入的自定义bean上发生.

Every things looks OK but when I change one of the child beans, the change does not take place on the imported custom bean in main Panel.

简短的答案如下:

  1. 在设计"视图中打开主面板.
  2. 转到导航器"标签.
  3. 右键单击Form MainPanel,然后选择Reload Form选项.
  4. 主面板已更新.

但是它不起作用,并且关于此问题的错误报告,他们告诉它不是错误,而是与布局管理器相关的内容(尽管不是很清楚的解释):

However it doesn't work and it seems there is a long standing issue with a bug report on this matter where they told it's not a bug but a layout manager related stuff (not very clear explanation though):

"问题是MyFrame中的jPanel1生成了布局代码 (GroupLayout)-设置布局会导致组件被删除.这 不幸的是,GUI构建器不知道存在其他容器 具有自己的布局,因为它只能通过自定义代码指定,而不能通过自定义代码指定 真正以GUI形式实例化.但是,您可以轻松解决此问题 -只需将MyFrame中的jPanel1的布局设置为Flowlayout.这是JPanel的默认布局,因此GUI构建器将不会生成任何布局. 布局代码和您的自定义面板将保持不变."

"The problem is that jPanel1 in MyFrame has a layout code generated (GroupLayout) - setting layout causes the components are removed. The GUI builder unfortunately does not know there is a different container with its own layout since it is specified only via custom code and not really instantiated in the GUI form. However you can fix this easily - just set the layout of the jPanel1 in MyFrame to Flowlayout. This is default layout of JPanel, so the GUI builder will not generate any layout code and your custom panel will be left intact."

因此,尚不清楚为什么GroupLayout导致此行为,但凭经验来解决它,您将必须:

So, it isn't very clear why GroupLayout causes this behavior but empirically to solve it you would have to:

  1. 为自定义面板设置其他布局管理器(即:FlowLayout或GridBagLayout).
  2. 将这些面板添加到主面板.如果它已经具有这些面板,则将其删除并再次添加.自定义面板至少要包含一个组件,这一点很重要.
  3. 完成前面的步骤后,可以将自定义面板布局管理器重新设置为GroupLayout(免费设计),并且仍然可以使用.
  4. 修改自定义面板后,必须对其进行编译(F9),并且需要通过Navigator Explorer浏览器更新主面板表单.

在继续之前,我建议您不要将布局管理器重新设置为GroupLayout(第3步),而应使用GridBagLayout.这也不是我的最爱,但是恕我直言,这是比GroupLayout更好的选择.您可能要查看在此处

Before go any further I'd suggest you don't set back the layout manager to GroupLayout (step 3) and use GridBagLayout instead. This is not my favorite either but IMHO it's a better choice than GroupLayout. You might want to take a look to the reasons against GroupLayout exemplified here and here (I'm sure there are more about it here in SO).

现在,请考虑以下示例(对不起,扩展名).

Now, please consider the following example (sorry for the extension).

转到新文件 Swing GUI窗体 JPanel .将其命名为CustomPanel

Go to New FileSwing GUI FormsJPanel. Name it CustomPanel

注意:它说默认"并不意味着它是默认的使用,而是默认的por JPanel类.正如我所说的,默认使用的布局管理器是GroupLayout.

Note: where it says Default it doesn't mean it's the default used but the default por JPanel class. As I've said the layout manager used by default is GroupLayout.

请注意,生成的代码应如下所示(完全没有GroupLayout):

Note the generated code should look like this (no GroupLayout at all):

public class CustomPanel extends javax.swing.JPanel {

    public CustomPanel() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();

        jButton1.setText("jButton1");
        add(jButton1);
    }// </editor-fold>

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    // End of variables declaration 
}

4.创建主面板

按照步骤1创建一个新的JPanel并将其命名为MainPanel.

4. Create the main panel

Follow step 1 to create a new JPanel and name it MainPanel.

或者使用调色板和Custom Bean,或者通过从Projects资源管理器中拖放来保存更改.

Either by using palette and Custom Bean or by drag and drop from the Projects explorer and save the changes.

注意:您可以根据需要调整此面板的大小.如果转到导航器"选项卡,则会在主面板内看到自定义面板:

Note: you can resize this panel if you want. If you go to Navigator tab you'll see the custom panel inside the main panel:

将布局重新设置为GroupLayout(自由设计),然后根据需要对其进行修改.保存更改并编译文件.

Set the layout back to GroupLayout (Free design) and modify it as needed. Save the changes and compile the file.

转到MainPanel设计视图:仍保持不变.转到导航器"选项卡,然后重新加载主面板的表单:

Go to MainPanel design view: it still unchanged. Go to Navigator tab and reload the main panel's form:

太棒了,主面板现在已更新!

Awesome, the main panel is now updated!

从现在开始,每次修改自定义面板时,我们都必须对其进行编译并重新加载主面板的表单(仅步骤6和7:)

From now, every time we modify the custom panels we have to compile them and reload main panel's form (only steps 6 and 7 :)

这篇关于如何刷新NetBeans中的自定义bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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