CardLayout面板在现有框架中显示错误

时间:2013-04-30 11:49:47

标签: java swing layout-manager cardlayout

我使用Netbeans 6.9.1中的设计视图设计了一个包含几个控件的框架。此外,我添加了一个空面板,我试图在按钮点击时切换几个摆动组件的显示。问题是,在按钮单击时,面板不显示任何内容。代码如下:

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    JPanel txtPanel = new JPanel();
    JPanel listPanel = new JPanel();

    JTextField txtfield = new JTextField("ABCDEFGHIJ", 20);
    txtPanel.add(txtfield);

    JList<String> list = new JList<String>();
    DefaultListModel<String> model = new DefaultListModel<String>();

    for (int i = 0; i < userCommands.size(); i++){
        model.addElement(userCommands.get(i));
    }
    list.setModel(model);
    listPanel.add(list);

    jPanel2.add(listPanel, "list");
    jPanel2.add(txtPanel, "text");

    //MainUI.getFrames()[0].add(jPanel2, BorderLayout.CENTER);

    itemStateChanged("text");
}

itemStateChanged的代码如下:

    public void itemStateChanged(String disp) {
    CardLayout cl = (CardLayout)(jPanel2.getLayout());
    cl.show(jPanel2, disp);
}

在第一段代码中,jPanel2被拖放到包含其他组件的框架上,我在此尝试实现的是,在按钮单击时,jPanel2应在文本字段之间切换并列出。但是目前,面板在点击按钮时没有显示任何内容。

2 个答案:

答案 0 :(得分:0)

在考虑jPanel2是否可以在不同的面板(不同的卡)之间切换之前,jPanel2是否随处可见?我可以看到显示jPanel2的唯一代码是将它添加到MainUI但是它被注释掉了吗?!那你怎么知道jPanel2里面的显示没有切换?

答案 1 :(得分:0)

我为你做了一个快速示例代码。看看。你需要将Card布局定义为全局并尝试。

import javax.swing.AbstractAction;

公共类TestPanel扩展了JPanel {

/**
 * Create the panel.
 */
JPanel panel;
CardLayout cl = new CardLayout();
public TestPanel() {
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 155, 0, 0};
    gridBagLayout.rowHeights = new int[]{94, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
    gridBagLayout.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
    setLayout(gridBagLayout);

    JButton btnNewButton = new JButton("New button");
    GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
    gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
    gbc_btnNewButton.gridx = 0;
    gbc_btnNewButton.gridy = 0;
    add(btnNewButton, gbc_btnNewButton);
    btnNewButton.setAction(new AbstractAction("New button") {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            JPanel txtPanel = new JPanel();
            JPanel listPanel = new JPanel();

            JTextField txtfield = new JTextField("ABCDEFGHIJ", 20);
            txtPanel.add(txtfield);

            JList<String> list = new JList<String>();
            DefaultListModel<String> model = new DefaultListModel<String>();

            for (int i = 0; i < 3; i++){
                model.addElement("Sanjaya");
            }
            list.setModel(model);
            listPanel.add(list);

            panel.add(listPanel, "list");
            panel.add(txtPanel, "text");

            //MainUI.getFrames()[0].add(jPanel2, BorderLayout.CENTER);

            itemStateChanged("list");

        }
    });

    JTextArea textArea = new JTextArea();
    textArea.setLineWrap(true);
    GridBagConstraints gbc_textArea = new GridBagConstraints();
    gbc_textArea.insets = new Insets(0, 0, 5, 5);
    gbc_textArea.fill = GridBagConstraints.BOTH;
    gbc_textArea.gridx = 1;
    gbc_textArea.gridy = 0;
    add(textArea, gbc_textArea);

    panel = new JPanel(cl);
    GridBagConstraints gbc_panel = new GridBagConstraints();
    gbc_panel.insets = new Insets(0, 0, 5, 0);
    gbc_panel.fill = GridBagConstraints.BOTH;
    gbc_panel.gridx = 2;
    gbc_panel.gridy = 0;
    add(panel, gbc_panel);

}
  public void itemStateChanged(String disp) {


        cl.show(panel, disp);
    }

}