我的项目有两个标签,一个叫做“ netwerken”,另一个叫做路由器。
在“ netwerken”选项卡中,我引用了一个名为“ Netwerken.java”的类,该类扩展了JPanel。在该小组中,我再次引用了也扩展了JPanel的类“ Subnets.java”。所以实际上我在选项卡的面板中创建了一个面板。但是我做错了什么,为什么屏幕上不会弹出我的按钮,文本字段,标签和文本区域?
附言我在Netbeans中完成了这个项目,并通过单击面板设计将面板彼此链接,然后添加自定义代码: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
}
最佳答案
您正在创建一个已经在其内部指定布局的类,然后在其外部指定一个覆盖它的布局。
在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而不是使用编辑器,则您会做得很好。