我的Java应用程序上有一个表单,主要是为用户提供数据库查询中的数据列表。一个想法是使用表格并使用我的结果集中的数据填充每一行。然而,在设计UI时,我的团队和我认为它看起来并不像我们想要的那样顺畅。所以我们认为我们会考虑在面板中创建结果的自定义视图。我们希望它看起来像:
因此,对于结果集的每个查询,它将看起来像这样,而不是表的行。
我遇到的问题是编码。我花了很多时间来研究如何将组件添加到JForm中。因为netbeans似乎默认将ui设置为grouplayout?所以我研究了如何使用以下方法添加1个面板:
javax.swing.JLabel idLbl;
javax.swing.JLabel jLabel1;
javax.swing.JLabel jLabel3;
javax.swing.JLabel jLabel5;
javax.swing.JLabel jLabel7;
javax.swing.JPanel jPanel1;
javax.swing.JLabel prefContactLbl;
javax.swing.JLabel propertyLabel;
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
idLbl = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
propertyLabel = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
contactLabel = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
prefContactLbl = new javax.swing.JLabel();
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel1.setText("Enquiry Id:");
jLabel1.setName("jLabel"+i);
idLbl.setText("jLabel2");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel3.setText("Property:");
propertyLabel.setText("A property Address in some town with a postcode");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel5.setText("Contact:");
contactLabel.setText("A Persons Name ( 01010100011)");
jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel7.setText("Prefered Contact:");
prefContactLbl.setText("Email/Phone");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(idLbl)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(propertyLabel))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel7)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(prefContactLbl)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(contactLabel)))
.addContainerGap(20, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(idLbl)
.addComponent(jLabel3)
.addComponent(propertyLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(prefContactLbl)
.addComponent(jLabel5)
.addComponent(contactLabel))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(10 , 10, 10)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(100, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(570, Short.MAX_VALUE))
);
我遇到的主要问题是动态添加1个以上我完全不确定如何去做,因为我能找到的就是设置布局而不是更新它。我不能设置多少.addcomponent作为它的动态。真的很困惑如何解决这个问题。
很抱歉,如果很难弄清楚我想要的东西更容易想到它,但把它写成文字是一场噩梦。
答案 0 :(得分:8)
通过更多地封装东西开始将使您的生活更轻松。创建JPanel
的子类,表示一个查询条目的视图:EnquiryPanel
。 EnquiryPanel
中包含的代码几乎与您在上面粘贴的内容相匹配,但它会从您传入的构造函数参数中填充,并且它本身就是JPanel
。
public class EnquiryPanel extends JPanel {
public EnquiryPanel(Result dbResult) {
// your layout code from above, but this is the content pane and
// the fields are populated from the dbResult object
}
}
然后,您可以将EnquiryPanel
的实例动态添加到容器面板,该容器面板的布局是垂直BoxLayout。我在下面称这个容器面板ResultsPanel
。
public class ResultsPanel extends JPanel {
public ResultsPanel() {
// Layout our contents vertically
BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
this.setLayout(layout);
}
// method to iterate over the results from the database
// construct an EnquiryPanel and add that EnquiryPanel to ourselves
public void layoutEnquiryResults(ResultCollection results) {
for (Result result : results) {
EnquiryPanel eqPanel = new EnquiryPanel(result);
add(eqPanel);
}
revalidate();
}
}
最后,您需要向ResultsPanel
添加JFrame
的实例。
ResultsPanel resultsPanel = new ResultsPanel();
resultsPanel.layoutEnquiryResults(myResultSet);
getContentPane().add(resultsPanel);
答案 1 :(得分:1)
不要使用GroupLayout。这些限制通常太难以手动设置。
相反,我可能会使用垂直BoxLayout。添加每个面板后,不要忘记重新验证()父面板。