java新手,我的cardlayout中未切换面板

时间:2011-12-22 16:16:22

标签: java swing cardlayout

有人可以告诉我,我做错了吗?

我有..

    JComboBox comboBox = new JComboBox();

    comboBox.addItem("Factor 1");
    comboBox.addItem("Factor 2");
    comboBox.addItem("Factor 3");

    comboBox.setSelectedIndex(-1);

    studyPlanController.setFactors(comboBox);
    MyItemListener itemListener = new MyItemListener();

    comboBox.addItemListener(itemListener);
    itemListener.setFactors(cardFactors);

和..

import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JPanel;

public class MyItemListener implements ItemListener {
    JPanel factors;

    public void setFactors(JPanel factors) {
        this.factors = factors;
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        CardLayout cl = (CardLayout) (factors.getLayout());
        cl.show(factors, (String) e.getItem());
    }

}

如果在itemStateChanged()中放置断点,并且更改了combox,则会触发断点,但是cardlayout仍然不受影响。

1 个答案:

答案 0 :(得分:2)

请查看http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

请检查您是否将面板添加到卡片布局中,其名称与组合框中显示的名称相同。名字可能不匹配。