动态添加 JPanel 到 Jframe

发布于 2024-10-22 04:39:25 字数 5110 浏览 3 评论 0原文

我的 Java 应用程序上有一个表单,它基本上是为用户提供来自数据库查询的数据列表。一个想法是使用一个表并用结果集中的数据填充每一行。然而,在设计 UI 时,我和我的团队认为它看起来并不像我们想要的那么流畅。因此,我们认为我们应该考虑在面板中创建结果的自定义视图。我们希望它看起来像: Desired custom view

因此,它看起来像这样,结果集上的每个查询都有一个,而不是表格的行。

我遇到的问题是编码。我花了很多时间尝试练习如何向 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:
Desired custom view

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花期渐远 2024-10-29 04:39:25

从多封装一些东西开始会让你的生活变得更轻松。创建一个 JPanel 的子类,它表示一个查询条目的视图:EnquiryPanelEnquiryPanel 中的代码几乎与您上面粘贴的内容相匹配,只是它将根据您传入的构造函数参数自行填充,并且它将是一个 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);

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. The EnquiryPanel 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 a JPanel itself.

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
   }
}

You can then dynamically add instances of EnquiryPanel to a container panel whose layout is a vertical BoxLayout. I call this container panel ResultsPanel below.

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();
  }
}

Finally you would add an instance of ResultsPanel to your JFrame.

ResultsPanel resultsPanel = new ResultsPanel();
resultsPanel.layoutEnquiryResults(myResultSet);
getContentPane().add(resultsPanel);
拥有 2024-10-29 04:39:25

不要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文