简体   繁体   English

如何在运行时将JPanel添加到现有布局

[英]How to add a JPanel to an existing layout during runtime

I have a JPanel in a swing desktop application containing components that were laid out using the GUI builder in NetBeans. 我在Swing桌面应用程序中有一个JPanel ,其中包含使用NetBeans中的GUI构建器进行布局的组件。 In the generated source code, NetBeans uses the GroupLayout as Layout Manager. 在生成的源代码中,NetBeans使用GroupLayout作为布局管理器。 This JPanel is in a JScrollPane . JPanelJScrollPane

I have created another panel and manually handcoded the layout of the internal components of this second panel. 我创建了另一个面板,并手动对第二个面板的内部组件的布局进行了手工编码。 During runtime, I want to add this second panel to the existing GroupLayout that was created by NetBeans at the click of a button. 在运行时,我想将第二个面板添加到NetBeans通过单击按钮创建的现有GroupLayout中。

See the Image below: 请参见下图:

在此处输入图片说明

In the image above, there is a JButton labeled 'Enter' the red line below the button is where I want my new panel to appear when the button is clicked. 在上图中,有一个JButton标记为“ Enter”,该按钮下方的红线是我希望单击该按钮时出现的新面板的位置。

在此处输入图片说明

The above image illustrates what happens when I used the GroupLayout.replace(Component existingComponent, Component newComponent) method, but I don't want to use the replace method, I don't want to replace an existing panel, I just want to add the new panel to the existing layout. 上图显示了当我使用GroupLayout.replace(Component existingComponent, Component newComponent)方法时发生的情况,但是我不想使用replace方法,我不想替换现有面板,我只想添加将新面板添加到现有布局。 I can't seem to find any method in GroupLayout that does this. 我似乎在GroupLayoutGroupLayout执行此操作的任何方法。 Please help. 请帮忙。

Sorry the code for this question is too long to paste, I don't want to discourage answers, but I hope you understand what I am asking. 抱歉,此问题的代码太长,无法粘贴,我不想阻止答案,但我希望您理解我的要求。 Thanks. 谢谢。

One solution is to just add an empty panel to the space below the red line. 一种解决方案是在红线下方的空间中添加一个空面板。 Expand it the size you want. 扩大您想要的大小。 Then when you click the button, just add that panel to the already existing empty panel, and revalidate() and repaint() that empty main panel. 然后,当您单击按钮时,只需将该面板添加到已存在的空白面板中,然后对该空白的主面板进行revalidate()repaint()

Another solution is to us a CardLayout for that empty panel, and hand code an empty panel adding it to the first empty panel, then adding the panel you want to show to the main empty panel also. 另一个解决方案是为我们为该空面板提供CardLayout ,并手动编码一个空面板,将其添加到第一个空面板中,然后将要显示的面板也添加到主空面板中。 When the button is pressed, it switched the CardLayout to the panel you want show. 当按下按钮时,它将CardLayout切换到要显示的面板。

Solution 1. 解决方案1。

private void jButtonXActionPerformed(java.awt.event.ActionEvent e){
    jPanelX.add(new MyOherPanel());
    jPanelX.revalidate();
    jPanelX.repaint();
}

Solution 2. 解决方案2。

MyPanel myPanel = null;
public GUI(){
    intitComponents();
    jPanelX.setLayout(new CardLayout());
    jPanelX.add(new JPanel(), "emptyPanel");
    myPanel = new MyPanel();
    jPanelX.add(myPanel, "myPanel");
}

...
private void jButtonXActionPerformed(java.awt.event.ActionEvent e) {
    CardLayout layout = (CardLayout)jPanelX.getLayout();
    layout.show(jPanelX, "myPanel");
}

Use this code for replacing new JPanel to your existing JPanel. 使用此代码将新的JPanel替换为现有的JPanel。 NewPanelClass is the class where your new Five Textfield is placed and jpanel1 is current blank panel. NewPanelClass是放置新的Five Textfield的类,而jpanel1是当前的空白面板。 so called CurrentPanel() on buttonclick event and you can get your panel in blank panel position as simple as that..In Group Layout. 在buttonclick事件上称为CurrentPanel(),就可以使面板位于空白面板的位置,就像在面板布局中那样简单。

private void CurrentPanel()
    {
        NewPanelClass mpanel = new NewPanelClass();
        this.getContentPane().remove(0);
        jPanel1.removeAll();
        javax.swing.GroupLayout firstPanelLayout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(firstPanelLayout);
        firstPanelLayout.setHorizontalGroup(
                firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(firstPanelLayout.createSequentialGroup().addGap(1, 1, 1).addComponent(mExploredDBView, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addContainerGap(714, Short.MAX_VALUE)));
        firstPanelLayout.setVerticalGroup(
                firstPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(firstPanelLayout.createSequentialGroup().addGap(1, 1, 1).addComponent(mExploredDBView, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGap(1, 1, 1)));
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        SwingUtilities.updateComponentTreeUI(this.getContentPane());
    }

Thanks.. 谢谢..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM