我正在使用NetBeans开发一个java swing桌面应用程序,我试图通过添加一些自定义组件来自定义面板的布局,这些组件无法通过拖放在NetBeans设计视图中完成。在设计视图中添加和调整面板大小后,NetBeans将生成以下代码:
salesInfoPanel = new javax.swing.JPanel();
... 这里生成了一些其他东西,但它们太多而无法粘贴 ...
salesInfoPanel.setBackground(new java.awt.Color(255, 255, 255));
salesInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sales Info", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Adobe Caslon Pro", 0, 8))); // NOI18N
现在这是我想自己手工编码的部分
javax.swing.GroupLayout salesInfoPanelLayout = new javax.swing.GroupLayout(salesInfoPanel);
salesInfoPanel.setLayout(salesInfoPanelLayout);
salesInfoPanelLayout.setHorizontalGroup(
salesInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
salesInfoPanelLayout.setVerticalGroup(
salesInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 132, Short.MAX_VALUE)
);
布局代码结束
现在我的问题是我有一个方法来执行自定义布局,它将自定义组件添加到面板中的布局。但是这段代码无法集成,因为NetBeans已经完成了上面的布局。我只是希望能够在将面板添加到另一个面板和布局之前调用该方法,如下面NetBeans所做:
javax.swing.GroupLayout invoicePanelLayout = new javax.swing.GroupLayout(invoicePanel);
invoicePanel.setLayout(invoicePanelLayout);
invoicePanelLayout.setHorizontalGroup(
invoicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(invoicePanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(invoicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(invoicePanelLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(advertLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(32, 32, 32))
.addGroup(invoicePanelLayout.createSequentialGroup()
.addComponent(invoiceNoLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(invoiceNoValue, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())))
.addGroup(invoicePanelLayout.createSequentialGroup()
.addGroup(invoicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(invoicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(salesInfoPanel,<-------//Here is the panel
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(customerInfoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cashInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(invoicePanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(logoLabel)))
.addGap(0, 0, Short.MAX_VALUE))
);
对于长代码很抱歉,但我希望你理解我的意思谢谢。