Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使卡片布局与任意数量的卡片一起工作?_Java_Swing_User Interface_Cardlayout - Fatal编程技术网

Java 如何使卡片布局与任意数量的卡片一起工作?

Java 如何使卡片布局与任意数量的卡片一起工作?,java,swing,user-interface,cardlayout,Java,Swing,User Interface,Cardlayout,我试图使cardLayout能够处理任意数量的卡片,这意味着我需要某种循环来遍历我拥有的所有对象。现在我试着使用手动创建的JPanel,但一旦我在其中加入循环,它就不起作用了 @SuppressWarnings("serial") public class ClassCardLayoutPane extends JPanel{ JPanel cards; public ClassCardLayoutPane() { initialiseGUI(); } private void

我试图使cardLayout能够处理任意数量的卡片,这意味着我需要某种循环来遍历我拥有的所有对象。现在我试着使用手动创建的JPanel,但一旦我在其中加入循环,它就不起作用了

@SuppressWarnings("serial")

public class ClassCardLayoutPane extends JPanel{

JPanel cards;

public ClassCardLayoutPane() {
    initialiseGUI();
}

private void initialiseGUI() {
    String[] listElements = {"A2", "C3"};
    cards = new JPanel(new CardLayout());


    JLabel label = new JLabel("Update");
    add(label);

    JList selectionList = new JList(listElements);
    selectionList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent evt) {

            if (!evt.getValueIsAdjusting()) {
                label.setText(selectionList.getSelectedValue().toString());
                CardLayout cl = (CardLayout)(cards.getLayout());
                cl.show(cards, label.getText());


            }
        }

    });

    // The panels created by this loop don't work, the cards get stuck on the first one

    /*
    for (int i = 0; i < listElements.length-1; i ++) {

        JPanel temp = new JPanel();
        temp.add(new JLabel(i+""));
        cards.add(temp, listElements[i]);

    }*/


    JPanel card1 = new JPanel();
    card1.add(new JTable(20,20));

    JPanel card2 = new JPanel();
    card2.add(new JTable(10,20));

    cards.add(card1, listElements[0]);
    cards.add(card2, listElements[1]);

    //the panels here do work. I don't know what I'm doing wrong

    add(selectionList);
    add(cards);
}

public static void main(String[] args) {
    JFrame main = new JFrame("Win");
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setPreferredSize(new Dimension(1366, 768));
    main.getContentPane().add(new ClassCardLayoutPane());
    main.pack();
    main.setVisible(true);
}

}
好的,那么注释掉的for循环是什么对我不起作用,这让我很困惑?有人能给我解释一下为什么它不起作用,我怎样才能使它起作用?顺便说一句,listElements可以有不同的大小,这就是我正在尝试的工作,因为最终listElements将以LinkedList开始,所以当我为ListItems创建数组时,每次都会有不同的大小,因为我不知道会有多少项。有人能帮我做这件事吗?我的意思是,当我使用循环时,JPanel卡在第一张卡上,不再切换到下一张卡!没有错误消息,程序运行正常,但它没有做它的本意,即交换卡!请注意,当我单独执行这些操作时,程序工作得非常完美!多谢各位

好的,那么注释掉的for循环是什么对我不起作用,这让我很困惑

您应该做的第一件事是向循环中添加调试代码,以查看是否使用了唯一的卡名

如果这样做,您会注意到以下问题:

for (int i = 0; i < listElements.length-1; i ++) {
为什么要从列表长度中减去1?您只能向CardLayout添加一个面板

代码应为:

for (int i = 0; i < listElements.length; i ++) {
当代码没有按您认为的那样执行时,您需要添加调试代码或使用调试器逐步执行代码,以查看代码是否按预期执行


你不能总是盯着代码看。

错误信息是什么?它不起作用意味着什么?它记录一个错误,它冻结,它添加了太多,它添加了太少…寻求调试帮助的问题为什么这段代码不工作?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参见:如何创建。使用链接改进您的问题-不要通过评论添加更多信息。谢谢你好,很抱歉,它不工作,我的意思是当我使用循环时,JPanel卡在第一张卡上,不再切换到下一张卡!没有错误消息,程序运行正常,但它没有做它的本意,即交换卡!非常感谢。哦,我的上帝,我爱你。非常感谢你。