2

ContainerPanel是一个JPanel使用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;

}

我还创建了这个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
}

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

4

1 回答 1

5

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

阅读FaqFormCustomContainerBean

基本思想(除了创建一个BeanDescriptor是您将需要提供某种“内容”面板,可以在设计时添加其他内容。

现在,如果您有兴趣提供自定义模板,那是我以前没有做过的事情。

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

于 2012-08-18T20:32:52.540 回答