使用NetBeans GUI Builder从自定义JPanel继承

Cod*_*ice 2 java user-interface swing netbeans

ContainerPanel是一个JPanel使用a 的自定义类BorderLayout.SOUTH包含JPanel一个按钮.我希望它CENTER是另一个自定义的实例JPanel,比如说AbstractPanel,它提供了一个抽象方法,当单击该按钮时将调用该方法.我还想以JPanel编程方式(在运行时)设置它.到目前为止,我可以在以下代码中看到所有这些(其中一些是由NetBeans GUI Builder生成的):

package jpaneldemo;

import java.awt.BorderLayout;

public class ContainerPanel extends javax.swing.JPanel {

    public ContainerPanel() {
        initComponents();
    }

    public ContainerPanel(AbstractPanel abPanel) {
        initComponents();

        this.abPanel = abPanel;
        this.add(this.abPanel, BorderLayout.SOUTH);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        buttonPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonPanel.add(okButton);

        add(buttonPanel, java.awt.BorderLayout.PAGE_END);
    }

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
        this.abPanel.abstractMethod();
    }

    // Variables declaration - do not modify
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton okButton;
    // End of variables declaration

    private AbstractPanel abPanel = null;
Run Code Online (Sandbox Code Playgroud)

}

我还创建了这个AbstractPanel类:

package jpaneldemo;

public abstract class AbstractPanel extends javax.swing.JPanel {

    public AbstractPanel() {
        initComponents();
    }

    protected abstract void abstractMethod();

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }
    // Variables declaration - do not modify
    // End of variables declaration
}
Run Code Online (Sandbox Code Playgroud)

现在我想创建这个AbstractPanel类的子类,我可以在NetBeans GUI中编辑它.通常,我在"项目"窗口中右键单击包名称,然后导航到"新建 - > JPanel ..."以创建自定义JPanel.如何AbstractPanel在"新建"菜单中显示,以便我可以使用NetBeansGUI Builder编辑新类?还是有另一种方法来完成同样的事情?

Mad*_*mer 5

如果您打算提供一个"模板"组件,然后可以将其添加到调色板并包含在其他容器中,那么您可以.

阅读FaqFormCustomContainerBean

基本思路(除了创建a之外,BeanDescriptor您需要提供某种"内容"面板,其中可以在设计时添加其他内容.

现在,如果您对提供自定义模板感兴趣,那就是我之前没有做过的事情.

您可以尝试通过http://netbeans.org/competition/win-with-netbeans/customize-java-template.html阅读.它可能有点过时,但可能会帮助您朝着正确的方向前进