Swing的GroupLayout和setContentPane - 丢失组件?

时间:2012-04-20 19:20:28

标签: java swing grouplayout contentpane

经过实验,在我看来,Swing的GroupLayout往往会丢失用于在我的GUI中重复使用的组件。

但是我没有在文档中看到任何使这个一次性使用规则清晰的内容。这让我想知道我是否犯了错误,或者我是否是一个可怜的读者。

例如,我使用JButton(“Foo”)的GroupLayout制作JPanel。然后我创建另一个JPanel,其中同一个JButton的GroupLayout重命名为“Bar”。

如果我使用JFrame.setContentPane从第二个JPanel切换回第一个JPanel,我会在第一个JPanel中丢失JButton。

任何人都可以解释为什么它会丢失组件,而且,任何人都可以提供一种方法来克服丢失组件的倾向吗?

这是一个完整的SSCCE,证明了这个问题:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GroupLayoutTest {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            deployGroupLayoutTest();
        }
    });
}

static JPanel firstPanel;
static JButton jbtnActionLog;
static JFrame systemFrame;

public static void deployGroupLayoutTest() {
    systemFrame = new JFrame("Group Layout Test");
    systemFrame.setSize(300, 300);

    firstPanel = new JPanel();

    JMenuBar jmbSystem = new JMenuBar();

    JMenu jmuAction = new JMenu("Action");

    JMenuItem jmiActionLog = new JMenuItem("Login");
    jmuAction.add(jmiActionLog);

    jmbSystem.add(jmuAction);

    jbtnActionLog = new JButton("Login");
    jbtnActionLog.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setContentPaneToSecondPanel();
        }
    });

    systemFrame.setJMenuBar(jmbSystem);

    GroupLayout gl = new GroupLayout(firstPanel);
    firstPanel.setLayout(gl);
    gl.setAutoCreateContainerGaps(true);
    gl.setAutoCreateGaps(true);

    GroupLayout.ParallelGroup hGroup = gl.createParallelGroup(GroupLayout.Alignment.CENTER);
    hGroup
            .addComponent(jbtnActionLog);

    gl.setHorizontalGroup(hGroup);

    GroupLayout.SequentialGroup vGroup = gl.createSequentialGroup();
    vGroup
            .addComponent(jbtnActionLog);
    gl.setVerticalGroup(vGroup);

    systemFrame.getContentPane().add(firstPanel);
    systemFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    systemFrame.setLocationByPlatform(true);
    systemFrame.setVisible(true);

}

public static void setContentPaneToSecondPanel() {
    jbtnActionLog.setText("Logout");
    ActionListener[] listenerList = jbtnActionLog.getActionListeners();
    jbtnActionLog.removeActionListener(listenerList[0]);
    jbtnActionLog.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            systemFrame.setContentPane(firstPanel);
            systemFrame.revalidate();
        }
    });

    JPanel secondPanel = new JPanel();

    GroupLayout gl = new GroupLayout(secondPanel);
    secondPanel.setLayout(gl);
    gl.setAutoCreateContainerGaps(true);
    gl.setAutoCreateGaps(true);

    GroupLayout.ParallelGroup hGroup = gl.createParallelGroup(GroupLayout.Alignment.CENTER);
    hGroup
            .addComponent(jbtnActionLog);

    gl.setHorizontalGroup(hGroup);

    GroupLayout.SequentialGroup vGroup = gl.createSequentialGroup();
    vGroup
            .addComponent(jbtnActionLog);
    gl.setVerticalGroup(vGroup);

    systemFrame.setContentPane(secondPanel);
    systemFrame.revalidate();
}

}

1 个答案:

答案 0 :(得分:2)

我没有完成所有代码,但根本无法将单个Swing组件添加到多个父代。每个组件只能出现在Swing层次结构中的一个位置。所以代码

JPanel firstPanel = ...;
JPanel secondPanel = ...;
JButton button = ...;
firstPanel.add( button );
secondPanel.add( button );

将导致button仅包含在其中一个面板中,而不是两者中。这与GroupLayout无关。

相关的SO question包含指向Swing tutorial的链接:

  

每个GUI组件只能包含一次。如果组件已经在容器中并且您尝试将其添加到另一个容器,则该组件将从第一个容器中删除,然后添加到第二个