0

我使用 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应该在文本字段和列表之间切换。但目前,面板在按钮单击时不显示任何内容。

4

2 回答 2

0

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

于 2013-04-30T11:59:49.127 回答
0

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

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

}

于 2013-04-30T12:46:07.740 回答