JPanel不会出现在JTabbedPane中?

时间:2014-05-17 12:02:50

标签: java swing netbeans jpanel jtabbedpane

我的项目有两个标签,1个叫做“netwerken”,另一个叫做路由器。 在我的“netwerken”选项卡中,我引用了一个名为“Netwerken.java”的类,该类扩展了JPanel。在那个面板中,我另外引用了“Subnets.java”类,它也扩展了JPanel。实际上,我在一个选项卡的面板中制作了一个面板。但是我做错了什么,为什么我的屏幕上不会弹出我的按钮,文本字段,标签和文字?

P.S。我在Netbeans中创建了这个项目,并通过单击Panel设计将面板相互链接,然后添加自定义代码:new“nameOfClass”();

以下是我的JFrame的代码:

package subnetmanager;

public class Subnetmanager extends javax.swing.JFrame {

/**
 * Creates new form Subnetmanager
 */
public Subnetmanager() {
    initComponents();
}

/**
 * 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    databank = new javax.swing.JLabel();
    tabladen = new javax.swing.JTabbedPane();
    netwerken = new Netwerken();
    routers = new Routers();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    databank.setText("Databank");

    javax.swing.GroupLayout netwerkenLayout = new javax.swing.GroupLayout(netwerken);
    netwerken.setLayout(netwerkenLayout);
    netwerkenLayout.setHorizontalGroup(
        netwerkenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    netwerkenLayout.setVerticalGroup(
        netwerkenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 252, Short.MAX_VALUE)
    );

    tabladen.addTab("Netwerken", netwerken);

    javax.swing.GroupLayout routersLayout = new javax.swing.GroupLayout(routers);
    routers.setLayout(routersLayout);
    routersLayout.setHorizontalGroup(
        routersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 395, Short.MAX_VALUE)
    );
    routersLayout.setVerticalGroup(
        routersLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 252, Short.MAX_VALUE)
    );

    tabladen.addTab("Routers", routers);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(tabladen)
        .addGroup(layout.createSequentialGroup()
            .addComponent(databank)
            .addGap(0, 0, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(databank)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(tabladen))
    );

    pack();
}

public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Subnetmanager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Subnetmanager().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel databank;
private javax.swing.JPanel netwerken;
private javax.swing.JPanel routers;
private javax.swing.JTabbedPane tabladen;
// End of variables declaration                   
}

以下是我的“netwerken”级别的代码:

package subnetmanager;

public class Netwerken extends javax.swing.JPanel {

public Netwerken() {
    initComponents();
}

@SuppressWarnings("unchecked")                         
private void initComponents() {

    subnet = new Subnet();

    javax.swing.GroupLayout subnetLayout = new javax.swing.GroupLayout(subnet);
    subnet.setLayout(subnetLayout);
    subnetLayout.setHorizontalGroup(
        subnetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 200, Short.MAX_VALUE)
    );
    subnetLayout.setVerticalGroup(
        subnetLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 150, Short.MAX_VALUE)
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(subnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 200, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(subnet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(0, 150, Short.MAX_VALUE))
    );
}                       


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

最后但并非最不重要的是我的“子网”类的代码:

package subnetmanager;

public class Subnet extends javax.swing.JPanel {

/**
 * Creates new form Subnet
 */
public Subnet() {
    initComponents();
}

@SuppressWarnings("unchecked")                        
private void initComponents() {

    naamLabel = new javax.swing.JLabel();
    naam = new javax.swing.JTextField();
    wijzigNaam = new javax.swing.JButton();
    netwerkadresLabel = new javax.swing.JLabel();
    netwerkadres = new javax.swing.JTextField();
    wijzigNetwerkadres = new javax.swing.JButton();
    subnetmaskLabel = new javax.swing.JLabel();
    subnetmask = new javax.swing.JTextField();
    wijzigSubnetmask = new javax.swing.JButton();
    hostsLabel = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    hosts = new javax.swing.JTextArea();
    voegHostToe = new javax.swing.JButton();
    verwijderHost = new javax.swing.JButton();

    naamLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    naamLabel.setText("Naam:");

    wijzigNaam.setText("wijzig");

    netwerkadresLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    netwerkadresLabel.setText("Netwerkadres:");

    wijzigNetwerkadres.setText("wijzig");
    wijzigNetwerkadres.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            wijzigNetwerkadresActionPerformed(evt);
        }
    });

    subnetmaskLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    subnetmaskLabel.setText("Subnetmask:");

    wijzigSubnetmask.setText("wijzig");

    hostsLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    hostsLabel.setText("Hosts:");

    hosts.setColumns(20);
    hosts.setRows(5);
    jScrollPane1.setViewportView(hosts);

    voegHostToe.setText("Voeg host toe");

    verwijderHost.setText("Verwijder");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(netwerkadresLabel)
                .addComponent(naamLabel)
                .addComponent(subnetmaskLabel)
                .addComponent(hostsLabel))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(voegHostToe, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(verwijderHost, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(naam)
                        .addComponent(netwerkadres)
                        .addComponent(subnetmask))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(wijzigNaam))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(10, 10, 10)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(wijzigSubnetmask)
                                .addComponent(wijzigNetwerkadres, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addComponent(jScrollPane1))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(naamLabel)
                .addComponent(naam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(wijzigNaam))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(netwerkadresLabel)
                .addComponent(netwerkadres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(wijzigNetwerkadres))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(subnetmaskLabel)
                .addComponent(subnetmask, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(wijzigSubnetmask))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(hostsLabel)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(voegHostToe)
                .addComponent(verwijderHost))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
}

// Variables declaration - do not modify                     
private javax.swing.JTextArea hosts;
private javax.swing.JLabel hostsLabel;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField naam;
private javax.swing.JLabel naamLabel;
private javax.swing.JTextField netwerkadres;
private javax.swing.JLabel netwerkadresLabel;
private javax.swing.JTextField subnetmask;
private javax.swing.JLabel subnetmaskLabel;
private javax.swing.JButton verwijderHost;
private javax.swing.JButton voegHostToe;
private javax.swing.JButton wijzigNaam;
private javax.swing.JButton wijzigNetwerkadres;
private javax.swing.JButton wijzigSubnetmask;
// End of variables declaration                   
}

2 个答案:

答案 0 :(得分:1)

您正在创建一个已在其内部指定布局的类,然后在外部指定一个覆盖它的布局。

Subnet内你有

    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);

然后在Netwerken中用

覆盖它
        subnet = new Subnet();
//      GroupLayout subnetLayout = new GroupLayout(subnet);
//      subnet.setLayout(subnetLayout);
//      subnetLayout.setHorizontalGroup(subnetLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 200, Short.MAX_VALUE));
//      subnetLayout.setVerticalGroup(subnetLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 150, Short.MAX_VALUE));

然后再次,Netwerken内有

    GroupLayout layout = new GroupLayout(this);
    this.setLayout(layout);

然后用

覆盖它
//      GroupLayout netwerkenLayout = new GroupLayout(netwerken);
//      netwerken.setLayout(netwerkenLayout);
//      netwerkenLayout.setHorizontalGroup(netwerkenLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 395, Short.MAX_VALUE));
//      netwerkenLayout.setVerticalGroup(netwerkenLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 252, Short.MAX_VALUE));

删除我注释掉的行。

注意:如果您手工编写GUI而不是编辑器,那么您将自己做得很好。

答案 1 :(得分:0)

  

“我在Netbeans中创建了这个项目,并通过单击Panel设计将面板相互链接,然后添加自定义代码:new”nameOfClass“();”

除非完全必要,否则我会避免将自定义代码添加到自动生成的代码区域。在这种情况下,你应该真的知道你在做什么。如果您了解如何正确使用gui构建器工具,还有许多其他选项。

例如,您可以拖放面板表单(而不是尝试以编程方式实例化它们,在这种情况下,布局可能会影响可见的内容和不可见的内容 - 我不是GroupLayoup的专家,所以我不能指出你的问题)。

enter image description here


此外,您 无法使用默认GroupLayout。如果您了解不同布局管理器的工作方式,您只需在导航器或设计视图中右键单击容器,然后选择设置布局


如果您想在不使用选项卡的情况下切换视图,您还可以看到How to Use CardLayout with Netbeans GUI Builder的示例。


<强>更新

  

“我也会这样做,但问题是我需要编程我的项目,所以我可以添加许多子网到我的”netwerken“标签”

然后,您应该为netwerken面板使用不同的布局管理器。动态添加组件时GroupLayout不是最友好的。使用类似BoxLayout的布局。就像我上面说的那样,只需从导航器中选择面板,然后选择设置布局。如果您想在运行时添加新的SubNet,只需添加它,然后revalidate()repaint() metwerken面板。