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编辑新类?或者还有另一种方法可以完成同样的事情吗?
答案 0 :(得分:5)
如果您打算提供一个“模板”组件,然后可以将其添加到调色板并包含在其他容器中,那么您可以。
基本理念(除了创建BeanDescriptor
之外,您需要提供某种“内容”面板,其中可以在设计时添加其他内容。
现在,如果您对提供自定义模板感兴趣,那就是我之前没有做过的事情。
您可以尝试阅读http://netbeans.org/competition/win-with-netbeans/customize-java-template.html。它可能有点过时,但可能会帮助您朝着正确的方向发展