动态添加 JPanel 到 Jframe
我的 Java 应用程序上有一个表单,它基本上是为用户提供来自数据库查询的数据列表。一个想法是使用一个表并用结果集中的数据填充每一行。然而,在设计 UI 时,我和我的团队认为它看起来并不像我们想要的那么流畅。因此,我们认为我们应该考虑在面板中创建结果的自定义视图。我们希望它看起来像:
因此,它看起来像这样,结果集上的每个查询都有一个,而不是表格的行。
我遇到的问题是编码。我花了很多时间尝试练习如何向 JForm 添加组件。因为 netbeans 似乎默认将 ui 设置为组布局?所以我想出了如何添加 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 作为其动态。真的很困惑如何去做这件事。
抱歉,如果我很难理解我想要表达的内容,那么想起来更容易,但把它用语言表达出来就是一场噩梦。
I have a form on my Java application which basically is to provide the user with a list of data from a DB query. one idea was to use a table and populate each row with data from my resultset. However when designing the UI my team and i decided that it didnt look as smooth as we wanted. So we thought we would look into creating a custom view of the results in a panel. we wanted it to look something like:
So instead of rows of a table it will look like this with one for each enquiry on the resultset.
The problem i am having is coding this. I spent a good deal of time trying to workout how to add a component to a JForm. as netbeans seem to by default set up the ui as a grouplayout? so I worked out how to add 1 panel using:
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))
);
The main problem I am faced is adding more than 1 dynamically I am completly unsure how to go about it as all i can find is to set the layout not update it. And I cant set how many .addcomponent as its dynamic. Really confused on how to go about this.
Sorry if its hard to work out what I am trying to get across easier to think of it but putting it into words is a nightmare.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从多封装一些东西开始会让你的生活变得更轻松。创建一个
JPanel
的子类,它表示一个查询条目的视图:EnquiryPanel
。EnquiryPanel
中的代码几乎与您上面粘贴的内容相匹配,只是它将根据您传入的构造函数参数自行填充,并且它将是一个JPanel
本身。然后,您可以动态地将
EnquiryPanel
实例添加到布局为垂直 BoxLayout 的容器面板中。我在下面将此容器面板称为ResultsPanel
。最后,您将
ResultsPanel
的实例添加到JFrame
中。Starting off by encapsulating things a bit more will make your life much easier. Make a subclass of
JPanel
that represents the view for one enquiry entry:EnquiryPanel
. TheEnquiryPanel
would have code in it that nearly matches what you pasted above, except that it will fill itself in from the constructor parameters that you pass in and it will be aJPanel
itself.You can then dynamically add instances of
EnquiryPanel
to a container panel whose layout is a vertical BoxLayout. I call this container panelResultsPanel
below.Finally you would add an instance of
ResultsPanel
to yourJFrame
.不要使用 GroupLayout。这些约束通常太混乱,无法手动设置。
相反,我可能会使用垂直的 BoxLayout。添加每个面板后,不要忘记 revalidate() 父面板。
Don't use GroupLayout. The constraints are generally too confusing to set by hand.
Instead I would probably use a vertical BoxLayout. After you add each panel don't forget to revalidate() the parent panel.